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
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!
Link to comment
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.