[EDIT] sorry guys had to take this code off
Jo
XML and PHP
Moderator: General Moderators
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
XML and PHP
Last edited by YoussefSiblini on Tue Dec 18, 2012 3:33 am, edited 1 time in total.
Re: XML and PHP
+1 for the Star Wars/Star Trek references, -1 for mixing the two.
Don't remember if you can sort with XQuery (at least with 1.0) but you can stuff everything into a PHP array and sort that.
You might be able to sort on the $teams directly, actually, without the temporary $teamarray. Worth a shot.
Don't remember if you can sort with XQuery (at least with 1.0) but you can stuff everything into a PHP array and sort that.
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) {
// ...Re: XML and PHP
There are 10 types of people in this world, those who understand binary and those who don't
-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: XML and PHP
Hi Guys,
Thank you for your replies,
I used this code after having a look at what you said:
This worked, thanks to you guys
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).
Thank you for your replies,
I used this code after having a look at what you said:
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>';
Re: XML and PHP
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).
1. Returns a negative number if $a < $b
2. Returns a positive number if $a > $b
3. Returns zero if $a == $b
I suggest code like
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;
}-
YoussefSiblini
- Forum Contributor
- Posts: 206
- Joined: Thu Jul 21, 2011 1:51 pm
Re: XML and PHP
Yesssssssssssssssssssssssss
thank you very much, you are so good you even made me understand how every thing works