我正在尝试创建一个 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>";
?>
最佳答案
如果您计算已处理的图像数量,则可以使用 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>";
}
}
关于PHP while循环制作图像表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19623467/