我试图用来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";
Run Code Online (Sandbox Code Playgroud)
这就是我的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;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在一个表格单元格中显示多个图像?如果一个服务只有一个图像,那么我可以做到,但它有多个图像然后我不知道该怎么做.
更改您的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";
Run Code Online (Sandbox Code Playgroud)
然后使用这个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;
}
Run Code Online (Sandbox Code Playgroud)