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.

<?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>

    Don't terminate your for statement with a semi-colon. Instead wrap the lines that are to execute in each loop with braces:

    for($counter = 0; $counter <= 4; $counter++)
    [color=red][b]{[/b][/color]
      // lines to execute during each loop go here
    [color=red][b]}[/b][/color]
    

      Hi! That works great, thanks! Your edited code showed that I can use ++ to increase - nice, I hadn't realised that and I didn't like the other style. Thanks again!

        Don't forget to mark this thread resolved.

        Also, if you want more information about the various operators in PHP (such as ++, --, etc.) you should check out the manual pages linked at the top of this page: [man]operators[/man].

          Write a Reply...