我无法创建脚本,其中某个子类别的产品需要在某个DIV内打印。目前,我正在提取所需的所有数据,但目前发生的情况是,例如,如果我有4个类别,它会创建4次DIV,但DIV中的产品都是相同的,但我需要它们是特定于相关类别,希望有道理。这是我到目前为止:PHP foreach子类别问题
$categories = getCategories();
foreach($categories as $category)
{
echo '<div class="panel">
<div class="panel-wrapper"><pre>';
echo print_r(getProducts($category['id_category']));
echo '</pre>
<h2 class="title">ff</h2>';
echo '</div>
</div>';
}
$ categories = array('1','2','3'); 产品拉到这样
$i = 0;
while ($row = mysql_fetch_assoc($result))
{
$products[$i] = $row;
$i++;
}
return $products;
将返回:
Array
(
[0] => Array
(
[title] => Hat
[description] =>nice description
[visible] => 1
[date-added] => 2011-02-10 20:02:02
[date-updated] => 0000-00-00 00:00:00
[price] => 10
[id_category] => 2
)
[1] => Array etc etc etc
)
我很新的PHP,所以我不知道如何没有一个foreach这是我觉得我跑这个循环会错的。
在此先感谢!
你的getProducts()做了什么?现在看起来您正在为类别循环的每个迭代运行查询,如果您有很多类别,这可能会非常低效。 – 2011-02-13 00:48:12