I have an xml file which I’m reading with php. Currently I have the xml loaded and its content displayed nicely in a table.
Currently I’m trying to sum up all the values of the “points” attribute, calculate the average of this and display that number.
Any help would be most welcome, since at the moment I’m having to add all these numbers up on a calculator.
Basic structure of the xml, greatly simplifed:
<data>
...
<season id="1" foo1="bar1" points="1" />
<season id="2" foo2="bar2" points="2" />
<season id="3" foo3="bar3" points="4" />
<season id="4" foo4="bar4" points="0" />
...
</data>
My php:
<?php
$url = "data.xml";
$xml = simplexml_load_file($url);
// loop through xml
foreach($xml->season as $season)
{
...
echo "<tr>";
echo "<td>".$season["id"]."</td>";
echo "</tr>";
...
}
// end ofloop
...
// don't really know what to do here
// to get to the paragaph below:
<p>Average points = $average_points </p>
?>
1 Answer