计算XML元素并回显与计算的xml元素一样多的结果

时间:2015-09-19 14:02:29

标签: php html xml

我想帮助解决一个问题。我使用PHP解析来自XML API的一些数据。 这是我的代码:

<?php
$gigatools = simplexml_load_file('http://gigs.gigatools.com/u/A2LTD.xml');
echo "<table id='gigs_parse'>\n";

for($i = 0; $i < 12; $i++) {

    $event = $gigatools->event[$i];
    $day=$event->day;
    $month=$event->month;
    $name=$event->name;
    $venue=$event->venue;
    $city=$event->city;
    $country=$event->country;
    $image=$event->image; 

        if($month == 1){
        $maand = 'JAN';
        } else if($month == 2){
        $maand = 'FEB';
        } else if($month == 3){
        $maand = 'MAR';
        } else if($month == 4){
        $maand = 'APR';
        } else if($month == 5){
        $maand = 'MAY';
        } else if($month == 6){
        $maand = 'JUN';
        } else if($month == 7){
        $maand = 'JUL';
        } else if($month == 8){
        $maand = 'AUG';
        } else if($month == 9){
        $maand = 'SEP';
        } else if($month == 10){
        $maand = 'OCT';
        } else if($month == 11){
        $maand = 'NOV';
        } else if($month == 12){
        $maand = 'DEC';
    }

echo "<tr cellspacing='60' class='row'><td class='date'><span>",$day,"</span><br/>",$maand,"</td>\n";
echo "<td class='party'>",$name,"</td>\n";
echo "<td class='location'>",$venue,", ",$city,", ",$country,"</td>\n";

echo "<td class='image'>"."<img src='".$image."'>"."</td>";
}

echo "</table>";
?>

正如您所看到的,我正在使用“for”来获得12个结果。这是非常静态的,它会导致丢失一些信息或给我一些空单元格。我想做的是获得与API上的事件元素一样多的结果。提前谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用SimpleXML's count方法。

以下是一个例子: -

<?php
$fileHandler = simplexml_load_file("A2LTD.xml");
var_dump($fileHandler->event->count());
?>

对于您的文件,上面的代码将返回9,这是父event的节点数。