如果属性值等于变量值,则查看XML数据

时间:2016-01-09 08:13:29

标签: php xml sorting

我坚持一些非常简单的事情。

这是我的xml Feed:
http://xml.betfred.com/Horse-Racing-Daily.xml

这是我的代码

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

现在,基本上XML文件包含了今天正在发生的赛马列表 我调用函数的页面也声明了

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

然后调用函数的地方我有

<?php HRList5($viewbets);?>

我只是玩了一下,现在它在第一个<bet>节点中显示数据 但问题是它没有全部显示它们,它只是在页面上重复第一个。

我基本上需要查询xml feed&amp;如果事件 - &gt; bettype-&gt;属性() - &gt; {&#39; bettypeid&#39;} == $ viewbets我希望在页面上重复下注节点。

1 个答案:

答案 0 :(得分:1)

我不使用simplexml因此无法提供相关指导 - 但我会说,要在xml Feed中找到您应该使用XPath查询所需的元素和属性。以下代码有望在这方面有用,它可能很容易翻译成simplexml方法。

编辑: 而不是将每次下注定位为原始xpath确实导致问题,以下内容应该更有用。它以bettype为目标,然后处理子节点。

/* 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;