Page 1 of 1

XML and PHP

Posted: Mon Dec 17, 2012 5:56 am
by YoussefSiblini
[EDIT] sorry guys had to take this code off


Jo

Re: XML and PHP

Posted: Mon Dec 17, 2012 12:11 pm
by requinix
+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.

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) {
    // ...
You might be able to sort on the $teams directly, actually, without the temporary $teamarray. Worth a shot.

Re: XML and PHP

Posted: Mon Dec 17, 2012 2:47 pm
by VladSun

Re: XML and PHP

Posted: Mon Dec 17, 2012 4:05 pm
by YoussefSiblini
Hi Guys,
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>';
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).

Re: XML and PHP

Posted: Mon Dec 17, 2012 5:07 pm
by requinix
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).
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:
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;
}
Keep in mind that $a and $b are SimpleXMLElement objects: for most kinds of comparisons you'll have to cast whatever value (like $a->overallpoints) to a string first.

Re: XML and PHP

Posted: Mon Dec 17, 2012 5:16 pm
by YoussefSiblini
Yesssssssssssssssssssssssss :) thank you very much, you are so good you even made me understand how every thing works