1

I am attempting to create a PHP script that will build a table of images from a database. I'm having a hard time figuring out how to properly nest my while loops to make a 6x(how ever many are in the db) table. I understand the concept but I am new to PHP and just can't wrap my head around doing it here.

<?php 

mysql_connect("localhost","root","");
mysql_select_db("images");

$res=mysql_query("SELECT * FROM table1");
echo "<table>";
while($row=mysql_fetch_array($res)){

echo "<tr>";
echo "<td>";?>

<img src="<?php echo $row["images1"]; ?>" height="150" width="150">

<?php echo "</td>";
echo "</tr>";}
echo "</table>";
?>
1
  • Note that mysql_* functions are deprecated (see the red box); don't use them in new code. Commented Oct 27, 2013 at 21:00

1 Answer 1

1

if you keep a count of how many images you have processed you can use if($count % 6 == 0) to test if you are at the 6th item in the list. So your loop would look like the following:

$c = 0; //our count
while($row = mysql_fetch_array($res)){
  if(($count % 6) == 0){  // will return true when count is divisible by 6
    echo "<tr>";
  }
  echo "<td>";
  ?>
  <img src="<?php echo $row["images1"]; ?>" height="150" width="150">
  <?php
  echo "</td>";
  $c++;   // Note we are incrementing the count before the check to close the row
  if(($count % 6) == 0){
    echo "</tr>";
  }
}
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.