PHP while 循环来制作一个图像表


PHP while loop to make a table of images

我正在尝试创建一个PHP脚本,该脚本将从数据库构建图像表。我很难弄清楚如何正确嵌套我的 while 循环以制作 6x(db 中有多少)表。我理解这个概念,但我是PHP的新手,只是无法在这里做这件事。

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

如果您保留已处理的图像数量,则可以使用if($count % 6 == 0)来测试您是否位于列表中的第 6 项。因此,您的循环将如下所示:

$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>";
  }
}