【问题标题】:PHP while loop to make a table of imagesPHP while循环制作图像表
【发布时间】:2013-11-06 13:21:30
【问题描述】:

我正在尝试创建一个 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>";
?>

【问题讨论】:

  • 请注意,mysql_* 函数已被弃用(请参阅red box);不要在新代码中使用它们。

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


【解决方案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>";
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2015-12-06
    • 2016-11-27
    • 1970-01-01
    • 2017-04-07
    • 2011-06-02
    • 2013-03-28
    相关资源
    最近更新 更多