我需要帮助在PHP中呈现数据:
我想使用SQL查询的结果来填充表格,在4列网格中显示图像。这是我到目前为止所写的内容:
$result = mysql_query("SELECT * FROM gallery ORDER BY id ASC ");
while($row = mysql_fetch_array($result))
{
echo "<td align=\"center\" ><a href=\"upload_gallery/".$row['image_name']."\" rel=\"prettyPhoto[gallery1]\" title=\" \"><img src=\"upload_gallery/thumbnail/sml_".$row['image_name']."\" width=\"200\" height=\"170\" /></a></td>";
}
答案 0 :(得分:1)
<table><tr>
<?$result = mysql_query("SELECT * FROM gallery ORDER BY id ASC ");
$result =mysql_fetch_array($result)
$count=0;
for($row =0; $row<count($result); $row++)
{
$count++
if($count==4){
echo '</tr><tr>';
$count=0;
}
echo "<td align=\"center\" ><a href=\"upload_gallery/".$row['image_name']."\" rel=\"prettyPhoto[gallery1]\" title=\" \"><img src=\"upload_gallery/thumbnail/sml_".$row['image_name']."\" width=\"200\" height=\"170\" /></a></td>";
}
?>
</tr>
</table>
答案 1 :(得分:1)
你可以按照弗雷德里克的建议做一个for循环:
$num_rows = mysql_num_rows($result);
for($i = 0; $i < $num_rows; $i++)
{
// Check if beginning of row
if($i % 4 == 0)
{
//If not the first row, end the last row first
if($i > 0)
{
echo "</tr>";
}
echo "<tr>";
}
$row = mysql_fetch_assoc($result);
//Output each individual image
echo "<td> {*EACH IMAGE code*}</td>";
}
这应该会给你一个想法。
答案 2 :(得分:0)
试试这个:
$arr =array("Test1","Test2","Test3","Test4","Test5","Test6","Test7","Test8","Test9","Test10");
echo "<table><tr>";
for($i = 1 ; $i <= count($arr) ; $i++)
{
echo"<td>".$arr[$i-1]."</td>";
$last = $i % 4 == 0? "<tr/><tr>":"";
echo $last;
}
echo "</table>";
答案 3 :(得分:0)
试试这个,希望它适合你,
while($row = mysql_fetch_array($result))
{
$data[]= $row['image_name'];## Pass the image data to new array
}
$ImagePerRow = 4;### Set the value of image per table row
echo "<table border='1'><tr>";
foreach($data as $key => $ImageName)
{
if(($key+1) == $ImagePerRow)## if key index+1 is equal to ImagePerRow
{
echo "<td align=\"center\" ><a href=\"upload_gallery/".$ImageName."\" rel=\"prettyPhoto[gallery1]\" title=\" \"><img src=\"upload_gallery/thumbnail/sml_".$ImageName."\" width=\"200\" height=\"170\" /></a></td></tr><tr>";## then close the table row and start a new table row
$ImagePerRow+=4;### Add a value of 4 in order to increment the imageperRow value on the next loop
}else{
echo "<td align=\"center\" >".$ImageName."</td>";### else echo the normal table data
}
}
echo "</tr></table>";