0

我需要帮助在 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>";
} 
4

4 回答 4

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>
于 2012-06-07T05:12:07.467 回答
1

您可以按照 Frederick 的建议执行 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>";
}

这应该给你的想法。

于 2012-06-07T05:15:35.697 回答
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>";
于 2012-06-07T05:31:11.880 回答
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>"; 
于 2012-06-07T05:55:38.797 回答