![](/img/trans.png)
[英]Create Different number of columns each row in a table with PHP and MYSQL
[英]Rearranging mysql data to fit a 4 column table where each row has a different number of columns (using colspan)
我正在尝试为一个由风景和个人资料照片组成的朋友整理一个照片集。 我有一个SQL表,其中包含照片详细信息,其中包括一列,说明照片是水平的还是垂直的。
表格行分为3:表格的第一行,显示4张垂直图片。 第二行显示1张垂直图片,1张水平差为2的水平图片,然后是另一张垂直图片。 第三行显示4张垂直图片。
根据需要重复上述模式。
我可以使用以下代码将表格限制为4列,并为水平照片添加一个colspan,但是顺序不正确-也会错过第一条记录。
任何帮助,将不胜感激!
<?php
$cols = 4; // number of columns
$c = 1;
echo "<table border=1>";
echo "<tr>";
while ($row_photos = mysql_fetch_assoc($photos)) {
if ($row_photos['photo_type'] == "Vertical") {
echo "<td>".stripslashes($row_photos['photo_file'])." ".stripslashes($row_photos['photo_type'])."</td>";
}
elseif ($row_photos['photo_type'] == "Horizontal") {
echo "<td colspan=\"2\">".stripslashes($row_photos['photo_file'])." ".stripslashes($row_photos['photo_type'])."</td>";
$c++;
}
if ($c == $cols) {
echo "</tr><tr>";
$c = 1;
} else { $c++; }
}
echo "</tr>" ;
echo "</table>";
?>
有几个问题:
$c
更改$c
0或使用>
而不是==
; $c
加2而不是加1,一次在if
语句之前,一次在else
语句中;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.