Konstantinos Koletsas
Konstantinos Koletsas

Reputation: 239

Count XML elements and echo as much results as the xml elements counted

I would like some help with an issue I have. I parse some data from and XML API using PHP. This is my code:

<?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>";
?>

As you can see, I am using "for" to get 12 results. This is pretty static and it leads to missing some information or giving me some empty cells. What I would like to do is to get as much results as the event elements on the API . Thank you in advance

Upvotes: 0

Views: 34

Answers (1)

Akshay
Akshay

Reputation: 2229

You can use SimpleXML's count method.

Here's an example :-

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

For your file, the above code will return 9, which is the number of nodes with parent event.

Upvotes: 1

Related Questions