Jdsflash Posted December 1, 2009 Share Posted December 1, 2009 I want to loop through the first 5 records in my xml. How would I format this correctly. Its nto displaying any of my xml. <?php $Gadgets = simplexml_load_string(file_get_contents('http://www.flashmajic.com/gadgets.xml')); //foreach ($Gadgets->Site as $Site) for($x=0;$x<6;$x++) { echo '<tr>'; echo '<td valign="top">'; echo '<h5>'.$Site[$x]->Title.'</h5>'; echo '<h6><img src="'.$Site[$x]->Image.'"/></h6>'; echo '</td>'; echo '<td valign="top">'; echo '<h5>'.$Site[$x]->Title.'</h5>'; echo '<h6>'.$Site[$x]->description.'</h6>'; echo '<h6><a href="'.$Site[$x]->links.'">Add Module To Your Google Page.</h6></a>'; echo '</td>'; echo '</tr>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/183618-for-loop-question/ Share on other sites More sharing options...
rajivgonsalves Posted December 1, 2009 Share Posted December 1, 2009 $Site should be $Gadgets->Site Quote Link to comment https://forums.phpfreaks.com/topic/183618-for-loop-question/#findComment-969186 Share on other sites More sharing options...
Jdsflash Posted December 1, 2009 Author Share Posted December 1, 2009 THanks for you help. So my code should be like this? This still does not work right. I probally over looked some thing. Thanks for your help. <?php $Gadgets = simplexml_load_string(file_get_contents('http://www.flashmajic.com/gadgets.xml')); //foreach ($Gadgets->Site as $Site) for($x=0;$x<6;$x++) { echo '<tr>'; echo '<td valign="top">'; echo '<h5>'.$Gadgets->$Site[$x]->Title.'</h5>'; echo '<h6><img src="'.$Gadgets->$Site[$x]->Image.'"/></h6>'; echo '</td>'; echo '<td valign="top">'; echo '<h5>'.$Gadgets->$Site[$x]->Title.'</h5>'; echo '<h6>'.$Gadgets->$Site[$x]->description.'</h6>'; echo '<h6><a href="'.$Gadgets->$Site[$x]->links.'">Add Module To Your Google Page.</h6></a>'; echo '</td>'; echo '</tr>'; } ?> My xml page <Gadgets> <Site> <Title>Soccer</Title> <Image>screen.jpg</Image> <description> pictures</description> <links></links> </Site> <Site> <Title>Soccer</Title> <Image>screen.jpg</Image> <description> pictures</description> <links></links> </Site> </Gadgets> Quote Link to comment https://forums.phpfreaks.com/topic/183618-for-loop-question/#findComment-969267 Share on other sites More sharing options...
mikesta707 Posted December 1, 2009 Share Posted December 1, 2009 $Gadgets->$Site[$x]->XXX Wherever you see occurrences like that, it should be $Gadgets->Site[$x] The method you were using previously is using what are called variable variables, which is probably not what you want Quote Link to comment https://forums.phpfreaks.com/topic/183618-for-loop-question/#findComment-969271 Share on other sites More sharing options...
Jdsflash Posted December 1, 2009 Author Share Posted December 1, 2009 So remove the last part. "->node title" Doesn't that last part rerfer to the xml node im refrencing? If i remove it how will it know what node to display? The code below still does not work. Did i do it like you intended? <?php $Gadgets = simplexml_load_string(file_get_contents('http://www.flashmajic.com/gadgets.xml')); for($x=0;$x<6;$x++) { echo '<tr>'; echo '<td valign="top">'; echo '<h5>'.$Gadgets->$Site[$x].'</h5>'; echo '<h6><img src="'.$Gadgets->$Site[$x].'"/></h6>'; echo '</td>'; echo '<td valign="top">'; echo '<h5>'.$Gadgets->$Site[$x].'</h5>'; echo '<h6>'.$Gadgets->$Site[$x].'</h6>'; echo '<h6><a href="'.$Gadgets->$Site[$x].'">Add Module To Your Google Page.</h6></a>'; echo '</td>'; echo '</tr>'; } ?> xml structure <Gadgets> <Site> <Title>Soccer</Title> <Image>screen.jpg</Image> <description> pictures</description> <links></links> </Site> <Site> <Title>Soccer</Title> <Image>screen.jpg</Image> <description> pictures</description> <links></links> </Site> </Gadgets> Quote Link to comment https://forums.phpfreaks.com/topic/183618-for-loop-question/#findComment-969276 Share on other sites More sharing options...
rajivgonsalves Posted December 1, 2009 Share Posted December 1, 2009 your code should be... <?php $Gadgets = simplexml_load_string(file_get_contents('http://www.flashmajic.com/gadgets.xml')); //foreach ($Gadgets->Site as $Site) for($x=0;$x<6;$x++) { echo '<tr>'; echo '<td valign="top">'; echo '<h5>'.$Gadgets->Site[$x]->Title.'</h5>'; echo '<h6><img src="'.$Gadgets->Site[$x]->Image.'"/></h6>'; echo '</td>'; echo '<td valign="top">'; echo '<h5>'.$Gadgets->Site[$x]->Title.'</h5>'; echo '<h6>'.$Gadgets->Site[$x]->description.'</h6>'; echo '<h6><a href="'.$Gadgets->Site[$x]->links.'">Add Module To Your Google Page.</h6></a>'; echo '</td>'; echo '</tr>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/183618-for-loop-question/#findComment-969278 Share on other sites More sharing options...
Jdsflash Posted December 1, 2009 Author Share Posted December 1, 2009 Thats perfect. It makes sence to me too because the Site is not a variable its the node. THis is clicking now thanks alot! Quote Link to comment https://forums.phpfreaks.com/topic/183618-for-loop-question/#findComment-969286 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.