簡體   English   中英

使用MySQL數據填充HTML表格(表格單元格的多個圖像)

[英]Populate HTML table with MySQL data (multiple images for a table cell)

我正在嘗試使用來自我的數據庫的數據填充HTML表。 在這里,我已經在我的表中存儲了“服務”。 每項服務可能有多個圖像。 因此,當填充表格時,它應該有3個表格cells一個用於“服務名稱”,第二個用於“描述”,第三個用於圖像。

這是我的SQL查詢看起來像:

$prep_stmt = " SELECT s.id
                    , s.name
                    , s.description
                    , i.image
                    , i.image_path
                FROM  services s
                LEFT JOIN images i ON i.service_id = s.id";

這是我如何while這個樣子:

while ($stmt->fetch()) {        
    $html  = "<tr>\n";  
    $html .= "  <td><input type='checkbox'></td>\n";    
    $html .= "  <td>\n";    
    $html .= "      <a href='' class='name'>{$name}</a>\n";     
    $html .= "  </td>\n";   
    $html .= "  <td class='view_html'>{$description}</td>\n";   
    $html .= "  <td>\n";    
                --- My images should be display here ---- 
    $html .= "  </td>\n";   
    $html .= "</tr>\n";                 

    //Add output to array
    $output[] = $html;  
}

我的問題是我如何在一個表格單元格中顯示多個圖像? 如果一個服務只有一個圖像,那么我可以做到,但它有多個圖像,然后我不知道該怎么做。

更改您的SQL代碼如下所示,然后嘗試

 $prep_stmt = " SELECT s.id
                    , s.name
                    , s.description
                    , (select group_concat(i.image_path,'/',i.image)  from images i where i.service_id = s.id) as img
                FROM  services s";

然后使用這個Html代碼

    while ($stmt->fetch()) {        
    $html  = "<tr>";  
    $html .= "  <td><input type='checkbox'></td>";    
    $html .= "  <td>";    
    $html .= "     <a href='' class='name'>{$name}</a>";     
    $html .= "  </td>";   
    $html .= "  <td class='view_html'>{$description}</td>";   

    $html .= "  <td>";
    $img_array=explode(",",$img);
    foreach($img_array as $im){
        if($im==''){
         $html .= " <img src='default.jpg'>";   
        }else{
         $html .= " <img src='{$im}'>";   
        }
    }
    $html .= "  </td>";

    $html .= "</tr>";                 

    //Add output to array
    $output[] = $html;  
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM