php - 使用 MySQL 数据填充 HTML 表格(表格单元格的多个图像)

标签 php html mysql

我正在尝试使用来 self 的数据库的数据填充 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;  
}

关于php - 使用 MySQL 数据填充 HTML 表格(表格单元格的多个图像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31107443/

相关文章:

html - token 数组的 Xslt 大小( token 化、 token 数量)

mysql - 缓慢的 MySQL 替换/复杂查询

php - SQL - 获取行数 WHERE "something = something"但不受 LIMIT 限制

php - PHP 中的动态常量?

php - 这个 Laravel 路由声明到底意味着什么?

PHPMD 捕获/抑制 fatal error

javascript - jQuery 多次执行脚本

javascript - 在 HTML/JS 中使用一个 md-switch 来切换一组其他 md-switch

javascript - 从 ajax 生成的 php 文件中调用 jquery 对话框

mysql - 在使用 BINARY 或不使用 Binary 的 MySQL 中,哪一个会更快?