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
jdsflash
Forum Newbie
Posts: 5 Joined: Mon Nov 30, 2009 11:59 am
Post
by jdsflash » Tue Dec 01, 2009 12:38 pm
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>';
}
?>
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Tue Dec 01, 2009 1:16 pm
What are you expecting and what are you getting ...?
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Tue Dec 01, 2009 1:46 pm
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
Post
by jdsflash » Tue Dec 01, 2009 3:15 pm
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>