I am trying to display images from a MySQL database in a table, three to a row. I'm able to do it but the problem is that my original code had it so if you had only one row in your MySQL table, it would run the code three times per row anyhow and leave two empty <td>'s (and leave one empty one if you had two rows). I was trying to run a loop that said if you have less than three rows remaining, run the loop for that many times and quit. I've looked over this code again and again and talked it out to myself but can't seem to find where the problem is. The error I get is that it runs the loop infinitely. I have two rows currently so what it's doing is running the loop twice per <tr> but forever.
I thought that adding 3 to the $rowcounter would stop the loop. The $rowcounter starts at 0, the loop runs through. 3 is added to the $rowcounter (making it 3) which would make it greater than $num_rows_temp (which would be two since there's only two rows) and the loop would not execute. Again, the problem must be in the first while loop because it's generating new rows in my table infinitely...Is there a reason that $rowcounter would not have had 3 added to it?
(Also, please ignore the variable declarations under the fetch_arrays. These are going to be using for functionality once I get the loop working. They look frivolous right now though...)
Can someone please help me figure out what I have wrong here? Thanks so much.
<?php
$rowcounter=0;
if($num_rows_temp==0){
echo "No pictures yet!";
}
else{
//BEGIN JOB POSTING
while($rowcounter<=$num_rows_temp){
$remaining=$num_rows_temp-$rowcounter;
$counter=1;
echo "<tr>";
//IF THE REMAINING AMOUNT OF ROWS IS LESS THAN THREE, RUN THE WHILE LOOP
//FOR THE AMOUNT OF REMAINING ROWS
if ($remaining>0 && $remaining<3){
while($counter<=$remaining){
$row_temp = mysqli_fetch_array($result_temp, MYSQL_NUM);
$id = $row_temp[0];
$location = $row_temp[1];
$name = $row_temp[2];
$email = $row_temp[3];
echo "<td>
<a href='".$location."'>
<img class=\"g-thumbnail\" src='".$location."' width=\"200\" height=\"200\" />
</a>
</td>";
$counter++;
} //END WHILE
} //END IF
else{
while($counter<=3){
$row_temp = mysqli_fetch_array($result_temp, MYSQL_NUM);
$id = $row_temp[0];
$location = $row_temp[1];
$name = $row_temp[2];
$email = $row_temp[3];
echo "<td>
<a href='".$location."'>
<img class=\"g-thumbnail\" src='".$location."' width=\"200\" height=\"200\" />
</a>
</td>";
$counter++;
} //END WHILE
} //END ELSE
echo "</tr>";
$rowcounter+3;
} //END WHILE
} //END ELSE
?>
$rowcounter+3;does nothing at all.