XML and PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

XML and PHP

Post by YoussefSiblini »

[EDIT] sorry guys had to take this code off


Jo
Last edited by YoussefSiblini on Tue Dec 18, 2012 3:33 am, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: XML and PHP

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: XML and PHP

Post by VladSun »

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

Post 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).
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: XML and PHP

Post 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.
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: XML and PHP

Post by YoussefSiblini »

Yesssssssssssssssssssssssss :) thank you very much, you are so good you even made me understand how every thing works
Post Reply