如何使用 concat 或任何类似函数实现此类别输出


How to achieve this category output using concat or any similar function

我想实现这个输出:

第1类

-字段1

第2类

-字段2

使用串联或任何函数。

我有一个嵌套循环,第一个将循环对类别进行 sql 查询,而第二个将通过查询循环,当找到类别匹配项时,它将相应地对它们进行分组。 现在我能够正确分组它,但它仅限于一个循环,对于大于 1 的循环,含义是, 它最终得到了最后一个类别并只显示它,所以我想知道是否有办法让我做到这一点。

代码是这样的:

 $unsize is the size of the array for the number of categories
 $un is the category array
for($x=0;$x<$unsize;$x++)
 {
  foreach(loop in db)
   {
     if($p->category == $un[$x])
      {
         do some really long code.
         summary:
         $a = "<tr><td>$un[$x]</td></tr>";
         $b = "<tr><td>$p->field</td></tr>";
      }
   }
 }

尝试使用这个:

  $a .= "<tr><td>Dog</td></tr><tr><td>Dogie</td></tr>";
  $a .= "<tr><td>Dog1</td></tr><tr><td>Dogie1</td></tr>";

最后以

猫1

-字段1

猫1

-字段3

其中字段 1 和字段 3 属于 Cat1

表具有 id,字段,排序顺序,类别。

您可以执行 sql 查询并按类别分组。

Select col, col2, col3 FROM table GROUP BY cat ORDER BY cat ASC