Chris Yates
Chris Yates

Reputation: 75

Sorting XML data based on Attribute Value

Need help guys Ive managed to find an easier XML feed to work with now and its perfect

The only problem is it doesnt order the results returned by date

here is the xml file

<event name="1m Maiden Stakes" eventid="5079164.20" date="20160105" time="1840" meeting="PORNICHET" venue="PORNICHET">
<bettype bet-start-date="20160105" bet-start-time="1840" ewreduction="5" ewplaceterms="3" eachway="1/5 Odds Place 1,2,3" suspended="false" name="Outright Betting" inrunning="0" bettypeid="25369679.20">
<bet jockey-silk="UqoAoo" name="Vision D'or" short-name="Vision D'" id="264074276.20" price="5/4" priceDecimal="2.25" priceUS="125.00" active-price-types="SP,EP"/>
<bet jockey-silk="MqoAoo" name="Le Moulleau" short-name="Le Moulle" id="264074277.20" price="10/1" priceDecimal="11.00" priceUS="1000.00" active-price-types="SP,EP"/>
<bet jockey-silk="TiqGii" name="Brancaio" short-name="Brancaio" id="264074278.20" price="10/1" priceDecimal="11.00" priceUS="1000.00" active-price-types="SP,EP"/>
<bet jockey-silk="PqlAqq" name="Bracoeur" short-name="Bracoeur" id="264074279.20" price="16/1" priceDecimal="17.00" priceUS="1600.00" active-price-types="SP,EP"/>
<bet jockey-silk="LoqGoo" name="Never Compromise" short-name="Never Com" id="264074280.20" price="12/1" priceDecimal="13.00" priceUS="1200.00" active-price-types="SP,EP"/>
<bet jockey-silk="HrbAbr" name="Machistador" short-name="Machistad" id="264074281.20" price="9/1" priceDecimal="10.00" priceUS="900.00" active-price-types="SP,EP"/>
<bet jockey-silk="UkkRkk" name="Royal Hawk" short-name="Royal Haw" id="264074282.20" price="8/1" priceDecimal="9.00" priceUS="800.00" active-price-types="SP,EP"/>
<bet jockey-silk="AqqGoo" name="Normandy Kitten" short-name="Normandy" id="264074283.20" price="9/2" priceDecimal="5.50" priceUS="450.00" active-price-types="SP,EP"/>
<bet jockey-silk="OqpAii" name="Tour" short-name="Tour" id="264074284.20" price="25/1" priceDecimal="26.00" priceUS="2500.00" active-price-types="SP,EP"/>
<bet jockey-silk="UqhRhq" name="Beninio" short-name="Beninio" id="264074285.20" price="16/1" priceDecimal="17.00" priceUS="1600.00" active-price-types="SP,EP"/>
<bet jockey-silk="OqoGoo" name="Just You And Me" short-name="Just You" id="264074286.20" price="33/1" priceDecimal="34.00" priceUS="3300.00" active-price-types="SP,EP"/>
</bettype>
</event>
<event name="1m Stakes " eventid="5079167.20" date="20160105" time="1910" meeting="PORNICHET" venue="PORNICHET">
<bettype bet-start-date="20160105" bet-start-time="1910" ewreduction="5" ewplaceterms="3" eachway="1/5 Odds Place 1,2,3" suspended="false" name="Outright Betting" inrunning="0" bettypeid="25369682.20">
<bet name="Blue Kanto" short-name="Blue Kant" id="264074319.20" price="10/1" priceDecimal="11.00" priceUS="1000.00" active-price-types="SP,EP"/>
<bet jockey-silk="ToqGoo" name="Francky Minuty" short-name="Francky M" id="264074320.20" price="16/1" priceDecimal="17.00" priceUS="1600.00" active-price-types="SP,EP"/>
<bet jockey-silk="CmkAkk" name="Rizzichop" short-name="Rizzichop" id="264074321.20" price="12/1" priceDecimal="13.00" priceUS="1200.00" active-price-types="SP,EP"/>
<bet jockey-silk="YblQlb" name="Colibri Cael" short-name="Colibri C" id="264074322.20" price="12/1" priceDecimal="13.00" priceUS="1200.00" active-price-types="SP,EP"/>
<bet jockey-silk="CibGii" name="Ali Alexandra" short-name="Ali Alexa" id="264074323.20" price="12/1" priceDecimal="13.00" priceUS="1200.00" active-price-types="SP,EP"/>
<bet name="Nayfah" short-name="Nayfah" id="264074324.20" price="4/5" priceDecimal="1.80" priceUS="-80.00" active-price-types="SP,EP"/>
<bet jockey-silk="RhmHmh" name="Elusive Dancer" short-name="Elusive D" id="264074326.20" price="4/1" priceDecimal="5.00" priceUS="400.00" active-price-types="SP,EP"/>
<bet jockey-silk="GanAaa" name="Kersea" short-name="Kersea" id="264074327.20" price="20/1" priceDecimal="21.00" priceUS="2000.00" active-price-types="SP,EP"/>
<bet jockey-silk="SlbGll" name="La Reussite" short-name="La Reussi" id="264074328.20" price="20/1" priceDecimal="21.00" priceUS="2000.00" active-price-types="SP,EP"/>
<bet jockey-silk="UlqAlb" name="Dream Cat" short-name="Dream Cat" id="264074329.20" price="16/1" priceDecimal="17.00" priceUS="1600.00" active-price-types="SP,EP"/>
</bettype>
</event>

Here is my PHP code

$xmlData = 'http://xml.betfred.com/Horse-Racing-Daily.xml';
$xml = simplexml_load_file($xmlData);
$curdate = date('d/m/Y');
$i = 0;
    foreach ($xml->event as $event)
        if ($i < 5) {
        {
            //20160112
        $eventd = substr($event->attributes()->{'date'},6,2);
        $eventm = substr($event->attributes()->{'date'},4,2);
        $eventy = substr($event->attributes()->{'date'},0,4);
        $eventdate = $eventd.'/'.$eventm.'/'.$eventy;
        $eventid = $event->attributes()->{'eventid'};
        $eventtime = $event->attributes()->{'time'};
        $eventname = $event->attributes()->{'name'};
        echo "<a href=\"event/".$eventname."/".$eventid."\">".$event->attributes()->venue.' - '.$eventtime.' - '.$eventname."</a><br />".$eventdate."<br />";
        $i++;} }

at the minute its only displaying the first 5 results which is fine, however i need to arrange and display the data by the $eventtime value which is the $event->attributes()->{'time'}; value

Please help guys I'm so almost there after stressing so much over these xml feeds

Thanks in advance

Upvotes: 0

Views: 141

Answers (1)

Paul Carlton
Paul Carlton

Reputation: 2993

What I would do in this case, if I don't have a way to change how the data is being output by the database, I would load it into PHP as an associative array and sort the keys. Something like this:

<?php 

$xmlData = 'http://xml.betfred.com/Horse-Racing-Daily.xml';
$xml = simplexml_load_file($xmlData);
$curdate = date('d/m/Y');

$new_array = array();
$limit = 5;
$c = 0;
foreach ($xml->event as $event) {
    if ($limit == $c) {
        break;
    }
    $c++;
    $eventd = substr($event->attributes()->{'date'},6,2);
    $eventm = substr($event->attributes()->{'date'},4,2);
    $eventy = substr($event->attributes()->{'date'},0,4);
    $eventt = $event->attributes()->{'time'};
    $new_array[$eventy.$eventm.$eventd.$eventt] = array(
        'eventd' => $eventd,
        'eventm' => $eventm,
        'eventy' => $eventy,
        'eventt' => $eventt,
        'eventdate' => $eventd.'/'.$eventm.'/'.$eventy,
        'eventid' => $event->attributes()->{'eventid'},
        'eventtime' => $event->attributes()->{'time'},
        'eventname' => $event->attributes()->{'name'},
        'venue' => $event->attributes()->venue,
    );
}

ksort($new_array);
foreach ($new_array as $event_time => $event_data) {
    echo "<a href=\"event/".$event_data['eventname']."/".$event_data['eventid']."\">".$event_data['venue'].' - '.$event_data['eventtime'].' - '.$event_data['eventname']."</a><br />".$event_data['eventdate']."<br />";
}

Of course this is NOT TESTED.

EDIT - Tested and it works now.

The point is that you load the array first with the information you need, sort by the key of the associative array and then print out what you want. You could change the limit on either the full loaded list, or just the first 5 from the xml file. Good luck!

Upvotes: 0

Related Questions