Page 1 of 1

for loop question

Posted: Tue Dec 01, 2009 12:38 pm
by jdsflash
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.

Code: Select all

 
<?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>';
} 
?>
 

Re: for loop question

Posted: Tue Dec 01, 2009 1:16 pm
by John Cartwright
What are you expecting and what are you getting ...?

Re: for loop question

Posted: Tue Dec 01, 2009 1:46 pm
by AbraCadaver
Well, since you commented out the foreach() $Site hasn't been defined. You'll probably need to access $Gadgets->Site[$x].

-Shawn

Re: for loop question

Posted: Tue Dec 01, 2009 3:15 pm
by jdsflash
So i should do it like this below? Thanks for the help im lost with php

Code: Select all

 
<?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]->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>';
} 
?>
 
Heres the xml node structure

Code: Select all

 
<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>