Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/183618-for-loop-question/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/183618-for-loop-question/#findComment-969267
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/183618-for-loop-question/#findComment-969276
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/183618-for-loop-question/#findComment-969278
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.