for loop question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jdsflash
Forum Newbie
Posts: 5
Joined: Mon Nov 30, 2009 11:59 am

for loop question

Post 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>';
} 
?>
 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: for loop question

Post by John Cartwright »

What are you expecting and what are you getting ...?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: for loop question

Post by AbraCadaver »

Well, since you commented out the foreach() $Site hasn't been defined. You'll probably need to access $Gadgets->Site[$x].

-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
jdsflash
Forum Newbie
Posts: 5
Joined: Mon Nov 30, 2009 11:59 am

Re: for loop question

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