我对 Zend 框架还很陌生,并且已经搜索了好几天,但没有找到以下问题的示例:
我试图从下面的代码中获得的结果是一个列表,该列表将具有主类别及其下方的匹配子类别,并循环遍历我的所有主要类别。
例子:
电器(主要类别)
- 微波
- 火炉
电子(主要类别)
- 计算机
- 收音机
这是代码(同样我不知道该怎么做;我的想法是做 nested foreach()
,第二个foreach()
从第一个选择中获取主类别 id):
// Get Categories with Sub Categories
$catid = 0;
$selectmain = $this->dbhInstance->select()
->from(array('a' => 'code_sub_category'),
array('b.id as mainid', 'b.site_category'))
->join(array('b' => 'site_categories'),
'b.id = a.site_category_id')
->group("b.id")
->order("b.site_category");
$selectsub = $this->dbhInstance->select()
->from(array('a' => 'code_sub_category'),
array('a.id as subid', 'a.sub_category', 'a.site_category_id'))
->join(array('b' => 'site_categories'),
'b.id = a.site_category_id')
->where("a.site_category_id = '" . $catid . "'")
->order("a.sub_category");
$fetch = $this->dbhInstance->fetchAll($selectmain);
$fetch2 = $this->dbhInstance->fetchAll($selectsub);
//var_dump($fetch2);
$items = array();
$items2 = array();
foreach ($fetch as $key => $value) {
$catid = $value['id'];
$items = array_merge($items, array($key => $value));
foreach ($fetch2 as $key => $value) {
$items = array_merge($items, array($key => $value));
}
$this->view->getsubcategories = $items;
}
$this->view->getmaincategories = $items;
//End FULL Categories for My Categories