auto increment for simpleXML

主宰稳场 提交于 2019-12-24 07:38:09

问题


I have my simpleXML script creating new element's in a xml file but I need the attribute to be a auto incremented id

for example

<gig id="1">
        <date>December 19th</date>
        <venue>The Zanzibar</venue>
        <area>Liverpool</area>
        <telephone>Ticketline.co.uk</telephone>
        <price>£6</price>
        <time>Time TBA</time>   
    </gig>  

Is correct but when I create a new elemenet the id has to be written in by myself.

My code is as followed

 $line1 = $sxe->addChild('gig');
    $line1->addChild('id', HERE HERE HERE!!!!!!);
    $line1->addChild('date', $day . " , " . $month . " , " . $year);
    $line1->addChild('venue', $venue);
    $line1->addChild('area', $area);
    $line1->addChild('Link', $link);
    $line1->addChild('Price', $price);

were is says "HERE HERE HERE!!!!!" I need to add in the id , Can some one help ?

Also the id need to follow the highest number so say if the latest is 20 the new one has to be 21


回答1:


Assuming the gigs are in an array:

$num_gigs = count($gigs);
for ($i = 0; $i < $num_gigs; $i++)
{
    $line1 = $sxe->addChild('gig');
    $line1->addChild('id', $i);
    $line1->addChild('date', $day . " , " . $month . " , " . $year);
    $line1->addChild('venue', $venue);
    $line1->addChild('area', $area);
    $line1->addChild('Link', $link);
    $line1->addChild('Price', $price);

}


来源:https://stackoverflow.com/questions/2101885/auto-increment-for-simplexml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!