tags:

views:

226

answers:

2

The XML feed is located at: http://xml.betclick.com/odds%5Ffr.xml

I need a php loop to echo the name of the match, the hour, and the bets options and the odds links. The function will select and display ONLY the matchs of the day with streaming="1" and the bets type "Ftb_Mr3".

I'm new to xpath and simplexml.

Thanks in advance.

So far I have:

<?php
$xml_str = file_get_contents("http://xml.betclick.com/odds_fr.xml");
$xml = simplexml_load_string($xml_str);

// need xpath magic
$xml->xpath();

// display

?>
+1  A: 

I don't know how to work xpath really, but if you want to 'loop it', this should get you started:

<?php
$xml = simplexml_load_file("odds_fr.xml");

  foreach ($xml->children() as $child)
  {
    foreach ($child->children() as $child2)
    {
      foreach ($child2->children() as $child3)
      {
        foreach($child3->attributes() as $a => $b)
        {
          echo $a,'="',$b,"\"</br>";
        }

      }
    }
  }
?>

That gets you to the 'match' tag which has the 'streaming' attribute. I don't really know what 'matches of the day' are, either, but...

It's basically right out of the w3c reference: http://www.w3schools.com/PHP/php%5Fref%5Fsimplexml.asp

George Sisco
XPath is really simple, you should learn it.
Josh Davis
Read my other comment. I agree.
George Sisco
+3  A: 

Xpath is pretty simple once you get the hang of it

you basically want to get every match tag with a certain attribute

//match[@streaming=1]

will work pefectly, it gets every match tag from underneath the parent tag with the attribute streaming equal to 1

And i just realised you also want matches with a bets type of "Ftb_Mr3"

//match[@streaming=1]/bets/bet[@code="Ftb_Mr3"]

This will return the bet node though, we want the match, which we know is the grandparent

//match[@streaming=1]/bets/bet[@code="Ftb_Mr3"]/../..

the two dots work like they do in file paths, and gets the match.

now to work this into your sample just change the final bit to

// need xpath magic
$nodes = $xml->xpath('//match[@streaming=1]/bets/bet[@code="Ftb_Mr3"]/../..');

foreach($nodes as $node) {
    echo $node['name'].'<br/>';
}

to print all the match names.

linead
Hey, that's cool, and it makes sense. Thanks! I love w3schools, but those sorts of examples, for me, just usually don't make sense until I see a practical one like the poster above and your example for it. It's weird, but it's just the way I learn. Drives me nuts sometimes because a lot of people think you're not a good programmer if you don't learn the same way as everyone else. Anyway, not to rant, but I'm a bit dyslexic, so it's much easier to talk about something for me as a kick start. Somehow, after that, it's like a key unlocks something and I can get into the 'standard' references.
George Sisco
That XPath expression should really be `//match[@streaming = "1"][bets/bet/@code = "Ftb_Mr3"]` -- the node you're looking for is "match" and the rest are predicates, they should be treated as such
Josh Davis