【问题标题】:Giving ids to nested xml data attributes, in php在 php 中为嵌套的 xml 数据属性提供 id
【发布时间】:2013-01-24 18:31:50
【问题描述】:

我正在导入一个包含大量“市场”的数据馈送,我想要一个显示所有市场的主页,因此对于该 ID,使用 foreach 循环来浏览数据并在每个市场上进行上市。

每个市场都有一堆属性以及嵌套的参与者,然后我想为每个市场制作一个页面,显示每个参与者的一些信息。

所以用户会导航到 index.php > event.php?id101

这是我卡住的一点,我怎样才能将用户发送到正确的页面,我正在考虑使用

<?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>";
  } 
?>

但是我怎样才能设置一个 var $market_id(来自 xml 提要)添加到 url 的末尾,所以它会将我发送到正确的页面?

(f1_feed.xml 与实时 xml 提要相同,只是用于开发的本地)

我使用的提要是http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N

我使用 simplexml 引入的

【问题讨论】:

  • 你能展示一些你已经完成的代码吗?或者您希望这里的用户帮助您解决所有问题?
  • @ajreal,抱歉,请参阅修改后的内容

标签: php xml variables simplexml


【解决方案1】:

这对我有用;

$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']);
}

【讨论】:

  • 感谢您,抱歉这么晚才回复。随着您的评论“给出数组(无用的方式!)”是指第一个 $market_attributes 块还是第二个?
  • 我的意思是不要使用那部分,只使用上面的 oop 部分。
猜你喜欢
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 2016-05-08
相关资源
最近更新 更多