XML and PHP
Posted: Mon Dec 17, 2012 5:56 am
[EDIT] sorry guys had to take this code off
Jo
Jo
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
$teamarray = array();
foreach ($teams->team as $team) {
$teamarray[] = $team;
}
usort($teamarray, function($a, $b) {
return (string)$a->overallpoints - (string)$b->overallpoints;
});
foreach ($teamarray as $team) {
// ...Code: Select all
$teams = new SimpleXMLElement('store.xml', null, true);
$teamsNumber = $teams->count();
echo '
<table width="100%" border="1px grey solid;">
<tr>
<th>Team</th>
<th>Played</th>
<th>Won</th>
<th>Drawn</th>
<th>Lost</th>
<th>Overall Points</th>
</tr>
';
$teamarray = array();
foreach ($teams->team as $team) {
$teamarray[] = $team;
}
usort($teamarray, function($a, $b) {
return (string)$b->overallpoints - (string)$a->overallpoints;
});
foreach($teamarray as $team => $data) // loop through our books
{
$name = $data->name;
$played = $data->played;
$won = $data->won;
$drawn = $data->drawn;
$lost = $data->lost;
$overallpoints = $data->overallpoints;
$goals = $data->goals;
echo '
<tr>
<td>' . $name . '</td>
<td>' . $played . '</td>
<td>' . $won . '</td>
<td>' . $drawn . '</td>
<td>' . $lost . '</td>
<td>' . $overallpoints . '</td>
</tr>';
}
echo '</table>';
I didn't see that part in your post. Modify that anonymous function (the one given to usort()) so that it sorts properly. Three simple rules it needs to follow:YoussefSiblini wrote:but the only thing is: The teams that has the same number of Overall Points is not arranging properly like they should (the team with the most number of goals on top).
Code: Select all
if (same overallpoints) {
return $a's number of goals - $b's number of goals;
} else {
return $a's overallpoints - $b's overallpoints;
}