【发布时间】:2012-04-13 23:53:58
【问题描述】:
我想实现这个输出:
猫1
-field1
二类
-field2
使用连接或任何函数。
我有一个嵌套循环,第一个循环将循环一个针对类别的 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
-field1
猫1
-field3
其中field1和field3属于cat1
表格有一个 id、字段、排序顺序、类别。
【问题讨论】:
-
你能发布你的表结构吗?您可以通过 sql 查询本身来实现这一点。
-
该表包含类别和一个字段,我之前尝试使用按类别分组,但它对我来说不起作用,因为它只会显示每个类别的 1 个字段,我需要它来显示很多。