dietkinnie Posted March 28, 2009 Share Posted March 28, 2009 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 More sharing options...
RichardRotterdam Posted March 28, 2009 Share Posted March 28, 2009 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. Link to comment https://forums.phpfreaks.com/topic/151515-solved-simplexml-help/#findComment-795818 Share on other sites More sharing options...
dietkinnie Posted March 29, 2009 Author Share Posted March 29, 2009 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. Link to comment https://forums.phpfreaks.com/topic/151515-solved-simplexml-help/#findComment-796220 Share on other sites More sharing options...
RichardRotterdam Posted March 29, 2009 Share Posted March 29, 2009 <?php foreach($xml->game as $game) { foreach($game->alternatives->alternative as $alternative){ //here is where you can access your alternatives } } ?> Link to comment https://forums.phpfreaks.com/topic/151515-solved-simplexml-help/#findComment-796224 Share on other sites More sharing options...
dietkinnie Posted March 29, 2009 Author Share Posted March 29, 2009 <?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 !! Link to comment https://forums.phpfreaks.com/topic/151515-solved-simplexml-help/#findComment-796245 Share on other sites More sharing options...
RichardRotterdam Posted March 29, 2009 Share Posted March 29, 2009 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? Link to comment https://forums.phpfreaks.com/topic/151515-solved-simplexml-help/#findComment-796251 Share on other sites More sharing options...
dietkinnie Posted March 29, 2009 Author Share Posted March 29, 2009 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 Link to comment https://forums.phpfreaks.com/topic/151515-solved-simplexml-help/#findComment-796254 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.