simpleXML 的自动增量 [英] auto increment for simpleXML

查看:31
本文介绍了simpleXML 的自动增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的 simpleXML 脚本在 xml 文件中创建新元素,但我需要该属性是自动递增的 id

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

例如

<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>  

是正确的,但是当我创建一个新的 elemenet 时,id 必须由我自己写入.

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

我的代码如下

 $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);

有人说这里在这里!!!!!"我需要添加 id,有人可以帮忙吗?

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

此外,id 需要遵循最高数字,所以如果最新的是 20,那么新的必须是 21

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

推荐答案

假设演出在一个数组中:

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);

}

这篇关于simpleXML 的自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆