sam
sam

Reputation: 10074

Giving ids to nested xml data attributes, in php

I've got a data feed I'm importing that has a load of 'markets', I want to have a main page displaying all the markets, so for that id use a foreach loop to go through the data and on each market make a listing.

Each market has a bunch of attributes as well as nested participants, I want to then make a page for each market, that displays some information about each participant.

So the user would navigate to index.php > event.php?id101

This is the bit were ive become stuck, how can i send the user to the right page, I was thinking of using

<?php 

$xml = simplexml_load_file('f1_feed.xml');


  foreach($xml->response->williamhill->class->type->market as $event) {
    $event_attributes = $event->attributes();
    echo "<tr>";

      // EVENT NAME WRAPPED IN LINK TO EVENT
      echo "<td>";
        echo '<a href="event.php?id=' . $market_id . '">';
            echo $event_attributes['name'];
        echo "</a>";
      echo"</td>";

      // DATE
      echo "<td>";
        echo $event_attributes['date'];
      echo "</td>";

    echo "</tr>";
  } 
?>

but how can I set a var $market_id (from the xml feed) to add to the end of the url, so it sends me to the right page ?

(f1_feed.xml is the same as the live xml feed, its just local for development)

the feed I'm using is http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N

which im bring in using simplexml

Upvotes: 0

Views: 126

Answers (1)

Kerem
Kerem

Reputation: 11576

This worked for me;

$xml = simplexml_load_file("openbet_cdn.xml");
foreach($xml->response->williamhill->class->type->market as $market) {
    // that gives an object (native)
    $market_attributes = $market->attributes();
    printf("<a href=\"event.php?id=%s\">%s</a>\n", 
                $market_attributes->id, 
                $market_attributes->name);
    // that gives an array (useless way!)
    // $market_attributes = (array) $market->attributes();
    // printf("<a href=\"event.php?id=%s\">%s</a>\n", 
                // $market_attributes['@attributes']['id'], 
                // $market_attributes['@attributes']['name']);
}

Upvotes: 1

Related Questions