简体   繁体   English

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

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

I have 10 images in my database.我的数据库中有 10 张图像。 I would like to display 5 images per table row.我想在每个表格行中显示 5 张图像。

image1 - image 2 - image 3 - image 4 - image 5图 1 - 图 2 - 图 3 - 图 4 - 图 5

(new row) (新行)

image6 - image7 - image8 - image9 - image10 image6 - image7 - image8 - image9 - image10

How am I able to do this using MySQL and PHP?我怎样才能使用 MySQL 和 PHP 做到这一点?

My Code:我的代码:

$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>';  
}

As you can see, the results only display in one huge row.如您所见,结果仅显示在一大排中。 The thing I must add is the rows will need to expand with the result.我必须添加的是行将需要随着结果展开。

Example;例子; there maybe more than 10 images per result.每个结果可能有 10 个以上的图像。 There could be, say, 20 images, this would mean I would need 4 rows...可能有 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