XML Feed不会随机显示所有记录

时间:2016-01-09 12:26:12

标签: php xml filtering

我的XML Feed就在这里 http://xml.betfred.com/football-bonus.xml

这是我用于不同运动的代码,我刚刚复制到新页面

<?php
function FBList() {
$xmlData = 'http://xml.betfred.com/football-bonus.xml';
$xml = simplexml_load_file($xmlData);
$curdate = date('d/m/Y');

$new_array = array();

foreach ($xml->event as $event) {
//    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'};
    $new_array[$eventy.$eventm.$eventd.$eventt] = array(
        'eventd' => $eventd,
        'eventm' => $eventm,
        'eventy' => $eventy,
        'eventt' => $eventt,
        'eventdate' => $eventd.'/'.$eventm.'/'.$eventy,
        'eventid' => $event->attributes()->{'eventid'},
        'eventtime' => $event->attributes()->{'time'},
        'eventname' => $event->attributes()->{'name'},
        'venue' => $event->attributes()->venue,
        'riders' => $event->bettype->bet,
        'bettypeid' => $event->bettype->attributes()->{'bettypeid'},
    );
}

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("/"," ");
    $riderscnt = $event_data['riders']->count();
    $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['eventtime']."
</div>
<div class=\"unit-50\">
".$event_data['venue'].' - '.$event_data['eventname']."
</div>

<div class=\"unit-20\">
<a href=\"horse-racing/race/".$event_data['bettypeid'].'/'.$venue."/".$eventname."\" style=\"text-decoration:none;\"><img src=\"betnow.gif\" ></a><br />
</div>
</div>";

} 
}
}
?>

好吧现在忽略了在代码中有随机赛马相关参考的事实 2之间的XML结构是相同的 但是上面的代码只显示这些条目 1245年 - 威科姆诉阿斯顿维拉 1330年 - Linlithgow Rose诉Forfar 1400 - Carpi v Udinese 1500 - 巴塞罗那对格拉纳达 1600 - 里昂对特鲁瓦 1700年 - 佛罗伦萨诉拉齐奥 1715年 - 塞维利亚对毕尔巴鄂竞技队 1730年 - Hearts v Aberdeen 1830年 - 维多利亚吉马良斯诉Arouca 1900年 - 雷恩诉洛里昂 1930年 - 皇家马德里对拉科鲁尼亚队 1945年 - 罗马对AC米兰 2105 - Levante v Rayo Vallecano

我相信你会注意到TONS丢失了 任何帮助将不胜感激,因为这让我感到非常恼火

2 个答案:

答案 0 :(得分:2)

我意识到你已经有了答案,但我想我还是会在这里发布这个问题 - 它会从之前的其他帖子继续发布。

<?php

    echo '
    <style>
        #bfresults{
            width:40%;
            font-size:0.9rem;
            font-family:verdana,arial;
            box-sizing:border-box;  
        }
        #bfresults section{
            margin:0.5rem auto;
            padding:0.5rem;
            box-sizing:border-box;
        }
        #bfresults section:nth-of-type(odd){
            background:whitesmoke;  
        }
        #bfresults section h1{
            font-size:1.1rem;
            color:blue; 
        }
        #bfresults section h1 span{
            color:darkgray; 
        }
        #bfresults section h2{
            font-size:1rem;
            color:gray;
        }
    </style>';




    $eid=25573326.20;

    $events=(object)array(
        'horses'    =>  (object)array(
            'url'   =>  'http://xml.betfred.com/Horse-Racing-Daily.xml',
            'query' =>  '//event/bettype[ number( @bettypeid )="'.$eid.'" ]'
        ),
        'football'  =>  (object)array(
            'url'   =>  'http://xml.betfred.com/football-bonus.xml',
            'query' =>  '//event/bettype'
        )
    );

    #$obj=$events->horses;
    $obj=$events->football;



    /* create the DOM object & load the xml */
    $dom=new DOMDocument;
    $dom->load( $obj->url );

    /* 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( $obj->query );

    /* If the query was successful there should be a nodelist object to work with */
    if( $oCol ){

        echo "<div id='bfresults'>";

        foreach( $oCol as $node ) {
            echo '
                <section>
                    <h1>'.$node->parentNode->getAttribute('name').' - <span>[ '.$node->getAttribute('name').' ]</span></h1>
                    <h2>'.date( 'D, j F, Y H:i:s', strtotime( $node->getAttribute('bet-start-date').' '.$node->parentNode->getAttribute('time') ) ).'</h2>';

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

    } else {
        echo 'XPath query failed';  
    }
    $dom = $xp = $col = null;
?>

答案 1 :(得分:1)

在新阵列中创建条目时,您可以添加日期和时间作为键:

$new_array[$eventy.$eventm.$eventd.$eventt] = array(...

当您在同一日期和时间开始比赛时,它将覆盖旧条目。

也许试试

$eventid = $event->attributes()->{'eventid'};
$new_array[$eventy.$eventm.$eventd.$eventt.$eventid] = array(...

获取唯一密钥。