簡體   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