/    Sign up×
Community /Pin to ProfileBookmark

php foreach xml loop problem

So I have some code here that loops through an xml file. Here is the xml file.

[code=html]<?xml version=”1.0″?>
<events>
<event>
<startDateTime>2013-06-28T00:31:00Z</startDateTime>
<id>309893600</id>
<IsLive>Yes</IsLive>
<status>O</status>
<drawRotNum>25</drawRotNum>
<homeTeam type=”Team1″>
<name>AC Milan</name>
<rotNum>23</rotNum>
<score>1</score>
</homeTeam>
<awayTeam type=”Team2″>
<name>AS Roma</name>
<rotNum>24</rotNum>
<score>2</score>
</awayTeam>
<periods>
<period lineId=”103648769″>
<number>0</number>
<description>Game</description>
<cutoffDateTime>2013-06-28T00:31:00Z</cutoffDateTime>
<spreads>
<spread>
<awaySpread>1</awaySpread>
<awayPrice>105</awayPrice>
<homeSpread>-1</homeSpread>
<homePrice>-115</homePrice>
</spread>
<spread altLineId=”186287960″>
<awaySpread>1.25</awaySpread>
<awayPrice>-121</awayPrice>
<homeSpread>-1.25</homeSpread>
<homePrice>110</homePrice>
</spread>
</spreads>
<totals>
<total>
<points>1</points>
<overPrice>-105</overPrice>
<underPrice>-105</underPrice>
</total>
<total altLineId=”186287961″>
<points>0.75</points>
<overPrice>110</overPrice>
<underPrice>-124</underPrice>
</total>
</totals>
<moneyLine>
<awayPrice>112</awayPrice>
<homePrice>121</homePrice>
<drawPrice>131</drawPrice>
</moneyLine>
<teamTotals>
<homeTeamTotal>
<total>1</total>
<overPrice>-105</overPrice>
<underPrice>-105</underPrice>
</homeTeamTotal>
<awayTeamTotal>
<total>2</total>
<overPrice>-105</overPrice>
<underPrice>-105</underPrice>
</awayTeamTotal>
</teamTotals>
<maxBetAmount>
<spread>4000</spread>
<totalPoints>4000</totalPoints>
<moneyLine>2000</moneyLine>
<teamTotals>5000</teamTotals>
</maxBetAmount>
</period>
</periods>
</event>
</events>
[/code]

Here is the php file that processes the xml:

[code=php] <?php
$xml = simplexml_load_file(“xml2.xml”);

foreach ($xml->event as $feed) { ?>
<h1> <?php echo $feed->status; ?> </h1>

<?php } ?>[/code]

The code above works correctly as $feed->status correctly outputs “Yes” but when I try something $feed->rotNum or $feed->rotNum[‘1’] nothing is outputted. I can’t seem to figure out what the problem is. Can someone help me out with this?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@rh_lloydnorthovMay 28.2014 — I'd try:

$feed->homeTeam->rotNum

$feed->awayTeam->rotNum

given that the rotNum node is within a subnode (homeTeam or awayTeam) of event
Copy linkTweet thisAlerts:
@ytesfay80authorMay 28.2014 — Thanks a lot now that was the solution but I do have another question.

When I try to use the first node in the foreach loop it doesn't work.
[code=php] foreach ($xml->events as $feed[/code]

But it works fine when I try this line in which <event> is the second node of the xml document.
[code=php] foreach ($xml->event as $feed[/code]
×

Success!

Help @ytesfay80 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...