繁体   English   中英

MySQL - PHP:在表格行中显示结果(每行 5 个结果)

[英]MySQL - PHP: Display Results In Table Rows (5 results per row)

我的数据库中有 10 张图像。 我想在每个表格行中显示 5 张图像。

图 1 - 图 2 - 图 3 - 图 4 - 图 5

(新行)

image6 - image7 - image8 - image9 - image10

我怎样才能使用 MySQL 和 PHP 做到这一点?

我的代码:

$query = mysql_query("SELECT * FROM gallery WHERE gallery_number='$gallery_number'");
while($fetch = mysql_fetch_assoc($query)){

  $image_name = $fetch['image_name'];
  $image_location = $fetch['image_location'];

  echo '<table width="960">';
  echo '<tr>';
  echo '<td>'.'<img src="'.$image_location.'" alt="'.$image_name.'"/>'.'</td>'; 
  echo '</tr>';
  echo '</table>';  
}

如您所见,结果仅显示在一大排中。 我必须添加的是行将需要随着结果展开。

例子; 每个结果可能有 10 个以上的图像。 可能有 20 张图像,这意味着我需要 4 行...

    $query = mysql_query("SELECT * FROM gallery WHERE gallery_number='$gallery_number'");

    echo '<table width="960">';
    $i = 0; //first, i set a counter 
    while($fetch = mysql_fetch_assoc($query)){
    //counter is zero then we are start new row  
        if ($i==0){
            echo '<tr>';
        }
        //here we creating normal cells <td></td>
        $image_name = $fetch['image_name'];
        $image_location = $fetch['image_location'];
        echo '<td>'.'<img src="'.$image_location.'" alt="'.$image_name.'"/>'.'</td>';   
        //there is a magic - if our counter is greater then 5 we set counter to zero and close tr tag  
        if ($i>5){
            $i=0;
            echo '</tr>';
        };  
        $i++; //$i = $i + 1 - counter + 1
    }
    echo '</table>';  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM