1

I am having some trouble displaying XML values stored in my arrays. I successfully traversed the nodes, but I'm trying to echo the arrays all together. Here is my code:

<?php  
$other_feed_url = "http://xml.pinnaclesports.com/pinnaclefeed.aspx?sporttype=Football&sportsubtype=NFL";  
$xml2 = simplexml_load_file($other_feed_url);  
$AwayLine[] = (string)$xml2->spread_visiting;  
$HomeLine[] = (string)$xml2->home_visiting;  
$AwayMoneyLine[] = (string)$xml2->moneyline_visiting;  
$HomeMoneyLine[] = (string)$xml2->moneyline_home;  
$AwaySpreadAdjust[] = (string)$xml2->spread_adjust_visiting;  
$HomeSpreadAdjust[] = (string)$xml2->spread_adjust_home;  
$UnderAdjust[] = (string)$xml2->under_adjust;  
$OverAdjust[] = (string)$xml2->over_adjust;  
$TotalPoints[] = (string)$xml2->total_points;  
for($i=0;$i<20; $i++) {  
    foreach ($xml2->events->event as $event) {  
        $Spread = $event->periods->period[0]->spread;   
        $MoneyLine = $event->periods->period[0]->moneyline;  
        $TotalPoints = $event->periods->period[0]->total;      
        $AwayLine[] = $Spread->spread_visiting;   
        $HomeLine[] = $Spread->spread_home;  
        $AwayMoneyLine[] = $MoneyLine->moneyline_visiting;   
        $HomeMoneyLine[] = $MoneyLine->moneyline_home;  
        $UnderAdjust[] = $TotalPoints->under_adjust;   
        $OverAdjust[] = $TotalPoints->over_adjust;  
        $TotalPoints[] = $TotalPoints->total_points;  
    }  
    echo '<br>';  
    echo $AwayLine[$i];  
    echo '<br>';  
    echo $HomeLine[$i];  
    echo '<br>';  
    echo $AwayMoneyLine[$i];   
    echo '<br>';  
    echo $HomeMoneyLine[$i];  
    echo '<br>';  
    echo $TotalPoints[$i];  
}  
?>
5
  • What is the resulting output? What would be the required output? Commented Oct 30, 2012 at 3:05
  • Currently, only $AwayLine and the $HomeLine arrays are printed. For resulting output, I have one goal which is to be able to display all of the variables at the end. I did have nested foreach loops with $Spread,$MoneyLine and $TotalPoints, but I was still unable to print anything but $AwayLine and $HomeLine. Commented Oct 30, 2012 at 3:12
  • 1
    Did you try checking what $AwayMoneyLine and so on contain? Maybe they are just empty nodes. Try using var_dump($AwayMoneyLine) to see what they contain, or check the generated source for your script. Commented Oct 30, 2012 at 3:14
  • @Rob thanks for your help so far. The results from var_dump($AwayMoneyLine); did result in NULL values, which is what I was expecting. After taking out this code $Spread = $event->periods->period[0]->spread; the resulting code was blank, so the error must deal with $AwayMoneyLine. Any suggestions? Thanks. Commented Oct 30, 2012 at 3:25
  • @BlaineHurtado $event->periods->period[0]->moneyline doesn't exist, see my answer below. Commented Oct 30, 2012 at 3:29

1 Answer 1

1

You are setting $MoneyLine = $event->periods->period[0]->moneyline; but this does not exist in the XML document you are loading. When you then try to fetch $AwayMoneyLine[] = $MoneyLine->moneyline_visiting;, there is no resulting value. See below for an example of one of the XML nodes from the document you are parsing:

<event>
    <event_datetimeGMT>2012-11-02 00:25</event_datetimeGMT>
    <gamenumber>272519903</gamenumber>
    <sporttype>Football</sporttype>
    <league>NFL</league>
    <IsLive>No</IsLive>
    <participants>
        <participant>
            <participant_name>Kansas City Chiefs</participant_name>
            <contestantnum>301</contestantnum>
            <rotnum>301</rotnum>
            <visiting_home_draw>Visiting</visiting_home_draw>
        </participant>
        <participant>
            <participant_name>San Diego Chargers</participant_name>
            <contestantnum>302</contestantnum>
            <rotnum>302</rotnum>
            <visiting_home_draw>Home</visiting_home_draw>
        </participant>
    </participants>
    <periods>
        <period>
            <period_number>0</period_number>
            <period_description>Game</period_description>
            <periodcutoff_datetimeGMT>2012-11-02 00:25</periodcutoff_datetimeGMT>
            <period_status>I</period_status>
            <period_update>open</period_update>
            <spread_maximum>10000</spread_maximum>
            <moneyline_maximum>5000</moneyline_maximum>
            <total_maximum>3000</total_maximum>
            <spread>
                <spread_visiting>9.5</spread_visiting>
                <spread_adjust_visiting>-130</spread_adjust_visiting>
                <spread_home>-9.5</spread_home>
                <spread_adjust_home>120</spread_adjust_home>
            </spread>
            <total>
                <total_points>42.5</total_points>
                <over_adjust>-102</over_adjust>
                <under_adjust>-108</under_adjust>
            </total>
        </period>
    </periods>
</event>
Sign up to request clarification or add additional context in comments.

4 Comments

it was there not too long ago. Do you think it would be a good idea to put in some code like if(moneyline==NULL), assign the values to OFF for example? Thanks!
@BlaineHurtado not sure what you mean by that? You want to check and see if the node is there?
yes if you have any suggestions that would be great. Otherwise I have a few ideas.
A simple if (isset($event->periods->period[0]->moneyline)) would do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.