Jump to content

[SOLVED] simplexml help


dietkinnie

Recommended Posts

HI Guys, i am having a little trouble parsing the below xml feed.

 


<game id="3664066" date="20090325" time="1700">
−
<description>
<category id="SOCMENEURESPSEGB1" order="7200">Spa. Segunda B1</category>
Celta B - Valladolid B
</description>
<type id="0"/>
−
<alternatives>
<alternative odds="1.50" team="133702">1</alternative>
<alternative odds="3.50" team="133702">X</alternative>
<alternative odds="5.95" team="193111">2</alternative>
</alternatives>
</game>

 

I need to display the above xml feed in the following format:

 

game id , alternative odds, alternative team , alternative type[1]

game id , alternative odds, alternative team , alternative type[x]

game id , alternative odds, alternative team , alternative type[2]

 

I am currently using the below php code.

 

$xml = new SimpleXMLElement($source,null,true);

foreach($xml as $game) {

$game_id = $game['id'];
$odds = $game->alternatives->alternative['odds'];
$team = $game->alternatives->alternative['team'];
$odds_type = $game->alternatives->alternative;

echo "{$game_id} - {$odds} - {$team} - {$odds_type}<br>";


}

 

 

And that is only displaying the first alternative, in this case [1] and ignoring [X] and [2]

 

I hope that i have made my self clear.  :-\ .

 

Any advice would be greatly appreciated.

 

Thanks in advance!!

 

Link to comment
https://forums.phpfreaks.com/topic/151515-solved-simplexml-help/
Share on other sites

if you use print_r() on the simpleXML object you will get the following output

(
[@attributes] => Array
	(
		[id] => 3664066
		[date] => 20090325
		[time] => 1700
	)

[description] => SimpleXMLElement Object
	(
		[category] => Spa. Segunda B1
	)

[type] => SimpleXMLElement Object
	(
		[@attributes] => Array
			(
				[id] => 0
			)

	)

[alternatives] => SimpleXMLElement Object
	(
		[alternative] => Array
			(
				[0] => 1
				[1] => X
				[2] => 2
			)

	)

)

notice how alternative is an array?

thats why you are only getting one value. You need to use a loop within a loop to get those values.

if you use print_r() on the simpleXML object you will get the following output

(
[@attributes] => Array
	(
		[id] => 3664066
		[date] => 20090325
		[time] => 1700
	)

[description] => SimpleXMLElement Object
	(
		[category] => Spa. Segunda B1
	)

[type] => SimpleXMLElement Object
	(
		[@attributes] => Array
			(
				[id] => 0
			)

	)

[alternatives] => SimpleXMLElement Object
	(
		[alternative] => Array
			(
				[0] => 1
				[1] => X
				[2] => 2
			)

	)

)

notice how alternative is an array?

thats why you are only getting one value. You need to use a loop within a loop to get those values.

 

Hi Dj Kat,

 

Any chance you can show me what the code should look like as I am still stuck ?

 

Cheers,

 

Rob.

<?php
foreach($xml->game as $game) {
foreach($game->alternatives->alternative as $alternative){
            //here is where you can access your alternatives
}
}
?>

 

Hi Dj Kat,

 

First of all thanks for your swift reply.

 

Now i seem to have run into another problem.

 

Here is the code.

 

$xml = new SimpleXMLElement($source,null,true);

foreach($xml->game as $game) {
  $game_id = $game['id'];
  $odds = $game->alternatives->alternative['odds'];
  $team = $game->alternatives->alternative['team'];

    foreach($game->alternatives->alternative as $alternatives){

    echo "{$game_id} - {$odds} - {$team}  - {$alternatives}<br>";

    }
}

?>

 

 

And here is the output:

 

3664066 - 1.50 - 133702 - 1

3664066 - 1.50 - 133702 - X

3664066 - 1.50 - 133702 - 2

3664073 - 2.70 - 133707 - 1

3664073 - 2.70 - 133707 - X

3664073 - 2.70 - 133707 - 2

3664200 - 1.70 - 193144 - 1

3664200 - 1.70 - 193144 - X

3664200 - 1.70 - 193144 - 2

 

However the output should be displayed as:

 

3664066 - 1.50 - 133702 - 1

3664073 - 2.70 - 133707 - X

3664200 - 1.70 - 193144 - 2

 

Each alternative is being assigned 3 times to a specific odd.

 

Your expert advice would be greatly appreciated again!  ;)

 

Thanks again !!

 

And here is the output:

 

3664066 - 1.50 - 133702 - 1

3664066 - 1.50 - 133702 - X

3664066 - 1.50 - 133702 - 2

3664073 - 2.70 - 133707 - 1

3664073 - 2.70 - 133707 - X

3664073 - 2.70 - 133707 - 2

3664200 - 1.70 - 193144 - 1

3664200 - 1.70 - 193144 - X

3664200 - 1.70 - 193144 - 2

 

However the output should be displayed as:

 

3664066 - 1.50 - 133702 - 1

3664073 - 2.70 - 133707 - X

3664200 - 1.70 - 193144 - 2

 

Each alternative is being assigned 3 times to a specific odd.

 

Your expert advice would be greatly appreciated again!  ;)

 

Thanks again !!

Are you sure about that? those game_id's must come from somewhere. What does the complete xml look like?

Infact this is a different problem

 

Taking the below xml into consideration

 

<game id="3664066" date="20090325" time="1700">
−
<description>
<category id="SOCMENEURESPSEGB1" order="7200">Spa. Segunda B1</category>
Celta B - Valladolid B
</description>
<type id="0"/>
−
<alternatives>
<alternative odds="1.50" team="133702">1</alternative>
<alternative odds="3.50" team="133702">X</alternative>
<alternative odds="5.95" team="193111">2</alternative>
</alternatives>
</game>

 

This should be the output: where the game id is common , odds are respective to bet type i.e (1 or X or 2 )

 

3664066 - 1.50 - 133702 - 1

3664066 - 3.50 - 133707 - X

3664066 - 5.95 - 193144 - 2

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.