Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用MySQL数据填充HTML表格(一个表格单元格有多个图像)_Php_Html_Mysql - Fatal编程技术网

Php 用MySQL数据填充HTML表格(一个表格单元格有多个图像)

Php 用MySQL数据填充HTML表格(一个表格单元格有多个图像),php,html,mysql,Php,Html,Mysql,我试图用数据库中的数据填充HTML表。在这里,我在表中存储了“服务”。每个服务可能有多个映像。因此,在填充表格时,它应该有3个表格单元格一个用于“服务名称”,第二个用于“描述”,第三个用于图像 这是我的SQL查询,如下所示: $prep_stmt = " SELECT s.id , s.name , s.description , i.image

我试图用数据库中的数据填充
HTML
表。在这里,我在表中存储了“服务”。每个服务可能有多个映像。因此,在填充表格时,它应该有3个表格
单元格
一个用于“服务名称”,第二个用于“描述”,第三个用于图像

这是我的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;  
}
while($stmt->fetch()){
$html=“\n”;
$html.=“\n”;
$html.=“\n”;
$html.=“\n”;
$html.=“\n”;
$html.=“{$description}\n”;
$html.=“\n”;
---我的图像应该显示在这里--
$html.=“\n”;
$html.=“\n”;
//将输出添加到数组
$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;  
}
while($stmt->fetch()){
$html=“”;
$html.=”;
$html.=”;
$html.=”;
$html.=”;
$html.=“{$description}”;
$html.=”;
$img_数组=分解(“,”,$img);
foreach($img\u数组作为$im){
如果($im=''){
$html.=”;
}否则{
$html.=”;
}
}
$html.=”;
$html.=”;
//将输出添加到数组
$output[]=$html;
}

首先将这些值存储在数组中,然后使用该数组生成htmltable@FerozAkbar,谢谢你的评论。但我不知道怎么做。你能给我举个例子吗?谢谢你,我会把它拿出来的。拉动所有服务,然后在需要时在HTML创建时查询所有图像。按原样,每个ID将获得多行,我认为您不需要。如果我了解您在数据库中的操作,@chris85是正确的。在这里使用连接只会为与服务关联的每个图片提供多行服务。使用2个查询将允许您在每个服务的最后一列中放置多个图像。GROUP_CONCAT很酷。我不知道您可以返回这样的数组。更改了sql查询。再试一次,请解释一下更新后的mysql查询,以及为什么它在之前的一个查询中不起作用;将所有图片分组,这就是为什么你只得到一行。但实际上你想得到与服务ID相关联的图像。@Sameeraa4ever,还有一个问题。如果
$im
为空,是否有办法显示默认图像?