按参考代码分组图像

时间:2017-08-25 21:26:33

标签: php mysql

我有一张图片表,我需要列出产品的所有图片才能连续显示:

enter image description here

我有这个问题:

<?php
require_once 'assets/files/meekrodb.php';

$results = DB::query("SELECT * FROM images");
foreach ($results as $row) {
  echo $row['imagen'] . "<br>";
}
?>

向我展示了这一点:

enter image description here

我想按字段分组&#34;产品&#34;并显示如下: enter image description here

你能帮助我吗,提前谢谢!

1 个答案:

答案 0 :(得分:3)

您可以在获取结果时按产品分组:

$results = DB::query("SELECT * FROM images");

foreach ($results as $row) {
    // use product as key, add image to array at that key
    $products[$row['product']][] = $row['imagen'];
}

然后输出这样的组:

foreach ($products as $product => $images) {
    echo implode(', ', $images) . '<br>';
}