0

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
?>
2
  • 1
    You're not adding 3 to the rowcounter there, you're setting it to 3. Try doing "$rowcounter += 3;" instead. Commented Nov 25, 2012 at 7:53
  • @FreudianSlip Not setting it to 3 either. $rowcounter+3; does nothing at all. Commented Nov 25, 2012 at 7:56

1 Answer 1

2

Your $rowcounter+3; is not increasing $rowcounter. Change to-

$rowcounter +=3;

As is $rowcounter+3; would only work if you are echo'ing

echo $rowcounter+3;  // would echo 3 each time

Also you are setting $counter inside your while loop

while($rowcounter<=$num_rows_temp){
    $remaining=$num_rows_temp-$rowcounter;
    $counter=1;

So even though you increase it by doing

$counter++;

It will be set back to 1 once it starts the while() loop again. You need to declare this before the while().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.