PHP while循环来制作图像表

时间:2013-10-27 20:57:41

标签: php mysql image while-loop html-table

我正在尝试创建一个PHP脚本,用于构建数据库中的图像表。我很难弄清楚如何正确地嵌套我的while循环来制作6x(数据库中有多少个)表。我理解这个概念,但我是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>";
?>

1 个答案:

答案 0 :(得分:1)

如果您计算已处理的图像数量,可以使用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>";
  }
}