Jump to content

Easy Loop Problem -Help!


vikinggirl***

Recommended Posts

This is my first loop in php...
It is supposed to do the following:

1) Read some images into an array.
2) Create a table
3) Create x number of rows in the table (x = a variable set elsewhere. Right now it is hard coded.)
4) Each new row is to contain one randomly chosen image, from the array that was created in 1).

At the moment, the loop isn't looping!
It only runs one instance of the loop.
So there is only one row created I think, at least only one images is displayed.
(I don't know any way of stepping through php code, so i don't know how to troubleshoot any further!)

What is wrong with this simple loop? It should run 4 times because of the hardcoded values. 

[CODE]
<?php
$images=array('vulgare.png', 'blueberry.png' ,'kantarell.png',);?>

<?php echo "<table class=\"sidebartable\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" >";

for  ($counter = 0;
$counter <= 4;
$counter += 1);

$rand=rand(0,3);
$image=$images[$rand];

{echo "<tr><td>";
echo"<img src=$image>";
echo "</td></tr>";}
?>
</table>
[/CODE]
Link to comment
https://forums.phpfreaks.com/topic/28533-easy-loop-problem-help/
Share on other sites

[code]
<?php
$images=array('vulgare.png', 'blueberry.png' ,'kantarell.png',);

echo "<table class=\"sidebartable\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" >";
for($counter = 0; $counter <= 4; $counter++){
$image=$images[rand(0,3)];
echo "<tr><td>";
echo"<img src=$image>";
echo "</td></tr>";
}
?>
</table>
[/code]

EDITED BY thorpe; Please use long <?php tags within the [ code ] blocks, this enables syntax highlghting. You should be using it within your code anyway!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.