【发布时间】:2020-03-10 00:34:18
【问题描述】:
使用此代码:
<?php
$stmt = $pdo->prepare("SELECT pdimg1 FROM products WHERE pdcat LIKE 'fashion%'");
$stmt->execute();
$rows = $stmt->fetchAll();
$img1 = '';
foreach ($rows as $row) {
$Rpdimg1 = $row['pdimg1'];
$img1 .= $Rpdimg1;
}
UPDATED TO INCLUDE BELOW:
$stmt = $pdo->prepare("SELECT * FROM products WHERE pdcat LIKE 'collectibles%'");
$stmt->execute();
$rows = $stmt->fetchAll();
$img2 = '';
foreach ($rows as $row) {
$Rpdimg2 = htmlspecialchars($row['pdimg1']);
$img2 .= $Rpdimg2;
}
$cats = array(
array(
"title" => "FASHION",
"img" => $img1
),
array(
"title" => "COLLECTIBLES & ART",
"img" => $img2,
)
);
foreach ($cats as $cat): ?>
<?php echo $cat["title"]; ?>
<img src="images/<?php echo $cat["img"]; ?>">
<?php endforeach; ?>
我在 Firefox 的 Inspector Tools 中显示了这个结果:
<img src="images/shirt.jpgpants.jpg">
[But on the webpage, this displays as a broken image.]
但我想要是这样的结果:
<img src="images/shirt.jpg">
<img src="images/pants.jpg">
[However, I would like the above results displayed as IMAGES, not as html text.]
这两个图像是从我的数据库中回显的值。它们来自我的products 表:
pdid | pdimg1
-----------------
1 | shirt.jpg
2 | pants.jpg
基本上是这样
1) 我正在从我的数据库表products中选择行
2) 我将它们作为变量插入到数组中(用于其他目的)
3) 我正在使用 foreach 循环从该数组中回显这些变量
4) 现在的问题在于,这些 IMAGE 变量聚集在一起作为一个单字 (shirt.jpgpants.jpg) 而不是作为单独的值,并且不会被封装在它们自己的 img 标签中,而是在相同的img 标签。
5) 所以我在上面用粗体字展示了我想要的结果。我将如何实现这一目标?谢谢
【问题讨论】: