我有一个图像数据库,我想将其输出为库。。。
我想在每个图像自己的单元格<td>image1</td>
中显示每个图像,并将单元格数限制为每行7个。
<tr>
<td>image1</td><td>image2</td><td>image3</td><td>image4</td><td>image5</td><td>image6</td><td>image7</td>
</tr>
然后将创建一个新行,并继续输出所有图像。
<tr>
<td>image8</td>
等等。。
我知道如何进行查询,但我不知道如何将行组装成我想要的格式。
有人能帮我吗?我将不胜感激。谢谢
首先运行查询,然后在变量中获取mysql_fetch_assoc。然后回显表和tr的开始标记。然后创建查询assoc的foreach循环,作为另一个变量,如row,并在td中回显$row['image']。这将为数据库中的每个条目添加一个带有图像的新td。然后回显tr和表的结束标记。
$query = mysql_query("SELECT image FROM table_name");
$query_a = mysql_fetch_assoc($query);
echo "<table><tr>"
foreach ($query_a as $row)) {
echo "
<td>" . $row['image'] . "</td>
";
}
echo "</tr></table>";
未测试,但尝试类似的方法
<table>
<?php
// make sure there is at least 1 row first
$cnt = 0;
$while($row = mysql_fetch_assoc($query))
{
if(++$cnt == 1) {
echo '<tr>';
} elseif($cnt % 7 == 0) {
echo '</tr><tr>';
}
// show images in <td>'s
echo "<td>" . $row['image']. "</td>";
}
echo '</tr>';
?>
</table>
保持简单:
查询数据库以获得所需的信息,循环查看结果,在每个新循环中构造一个新行。
$sql = "Select * from table";
mysql_query($sql) or die(mysql_error());
if(mysql_num_rows !=0)
{
// Found at least one record, create table
echo "<table>";
echo "<tr>";
while($row = mysql_fetch_assoc($sql)
{
$cell_count = 0;
// Create a new row for each record found
echo "<td>" . $row['image_column']. "</td>";
$cell_count++;
if($cell_count == 7)
{
echo "</tr>";
echo "<tr>";
$cell_count = 0;
}
}
// Close the table
echo "</tr>";
echo "</table>";
}