0

I'm stuck on something extremely simple.

Here is my xml feed:
http://xml.betfred.com/Horse-Racing-Daily.xml

Here is my code

    <?php
function HRList5($viewbets) {
$xmlData = 'http://xml.betfred.com/Horse-Racing-Daily.xml';
$xml = simplexml_load_file($xmlData);
$curdate = date('d/m/Y');
$new_array = array();


foreach ($xml->event as $event) {
    if($event->bettype->attributes()->bettypeid == $viewbets){//$_GET['evid']){
//  $eventid = $_GET['eventid'];

//    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'};
    $eventid = $event->attributes()->{'eventid'};
    $betname = $event->bettype->bet->attributes()->{'name'};
    $bettypeid = $event->bettype->attributes()->{'bettypeid'};
    $betprice = $event->bettype->bet->attributes()->{'price'};
    $betid = $event->bettype->bet->attributes()->{'id'};
    $new_array[$betname.$betid] = array(
    'betname' => $betname,
        'viewbets' => $viewbets,
        'betid' => $betid,
        'betname' => $betname,
        'betprice' => $betprice,
        'betpriceid' => $event->bettype->attributes()->{'betid'},
    );
}

ksort($new_array);
$limit = 10;
$c = 0;
foreach ($new_array as $event_time => $event_data) {


//  $racedate = $event_data['eventy'].$event_data['eventm'].$event_data['eventd'];
    $today = date('Ymd');

    //if($today == $racedate){
    //  if ($limit == $c) {
    //    break;
    //}
    //$c++;



    $replace = array("/"," ");
//  $eventname = str_replace($replace,'-', $event_data['eventname']);
    //$venue = str_replace($replace,'-', $event_data['venue']);
    echo "<div class=\"units-row unit-100\">
    <div class=\"unit-20\" style=\"margin-left:0px;\">
".$event_data['betprice']."
</div>
<div class=\"unit-50\">
".$event_data['betname'].' - '.$event_data['betprice']."
</div>

<div class=\"unit-20\">
<a href=\"horse-racing/race/\" style=\"text-decoration:none;\"><img src=\"betnow.gif\" ></a><br />
</div>
</div>";
    }

}//echo "<a href=\"horse-racing\" style=\"text-decoration:none; line-height:25px;\"><strong>View ALL Horse Races</strong> <strong>&gt;&gt;</strong></a>";
//var_dump($event_data);
}
?>

Now basically the XML file contains a list of horse races that are happening today.
The page I call the function on also declares

<?php $viewbets = $_GET['EVID'];?>

Then where the function is called I have

<?php HRList5($viewbets);?>

I've just had a play around and now it displays the data in the first <bet> node but the issue is it's not displaying them ALL, its just repeating the 1st one down the page.

I basically need the xml feed queried & if the event->bettype->attributes()->{'bettypeid'} == $viewbets I want the bet nodes repeated down the page.

1 Answer 1

1

I don't use simplexml so can offer no guidance with that - I would say however that to find the elements and attributes you need within the xml feed that you ought to use an XPath query. The following code will hopefully be of use in that respect, it probably has an easy translation into simplexml methods.

Edit: Rather than targeting each bet as the original xpath did which then caused issues, the following should be more useful. It targets the bettype and then processes the childnodes.

/* The `eid` to search for in the DOM document */
$eid=25573360.20;

/* create the DOM object & load the xml */
$dom=new DOMDocument;
$dom->load( 'http://xml.betfred.com/Horse-Racing-Daily.xml' );

/* Create a new XPath object */
$xp=new DOMXPath( $dom );
/* Search the DOM for nodes with particular attribute - bettypeid - use number function from XSLT to test */
$oCol=$xp->query('//event/bettype[ number( @bettypeid )="'.$eid.'" ]');

/* If the query was successful there should be a nodelist object to work with */
if( $oCol ){
    foreach( $oCol as $node ) {
        echo '
            <h1>'.$node->parentNode->getAttribute('name').'</h1>
            <h2>'.date('D, j F, Y',strtotime($node->getAttribute('bet-start-date'))).'</h2>';

        foreach( $node->childNodes as $bet ){
            echo "<div>Name: {$bet->getAttribute('name')} ID: {$bet->getAttribute('id')} Price: {$bet->getAttribute('price')}</div>";   
        }
    }
} else {
    echo 'XPath query failed';  
}

$dom = $xp = $col = null;
Sign up to request clarification or add additional context in comments.

8 Comments

It works, thanks, however how can I get it to just display the attribute value i specify eg name / price / id ?
Rather than iterate through the entire attribute collection just target the ones you want
thanks, its displaying all data however for some strange reason its displaying each item 7 times before displaying the next
forgot to // the foreach statement lol works perfectly ! Many thanks !!1
The XPath query targets the bet node - it could be changed to target the bettype node instead and then you would iterate through the childNodes collection. Anyway, glad it helped - hopefully you can make use of the code :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.