我已经为magento创建了自定义模块,该问题在表中由类别和子类别组成,现在我要显示以下内容
Category 1
Subcategory 1
Subcategory 2
Subcategory 3
Category 2
Subcategory 1
Subcategory 2
Subcategory 3
Category 3
Subcategory 1
Subcategory 2
Subcategory 3
我的代码是
$collection = Mage::getModel('supportportal/supportportal')->getCollection();
// -> addFieldToFilter('parent_category_id', array('neq' => 0));
foreach ($collection as $data) {
$collection2 = Mage::getModel('supportportal/supportportal')->getCollection()
-> addFieldToFilter('category_id', array('eq' => $data->getData('parent_category_id')));
foreach ($collection2 as $data2) {
echo '<h4>'.$data2->getData('category_name').'</h4>';//display category having no parent
echo '<ul>';
echo '<li>
<a href="'.$data->getData('category_Url').'">'.$data->getData('category_name').'</a></li>';
//display category having parent
echo '</ul>';
}
}
我们的表结构是
fmequestion_category
CREATE TABLE `fmequestion_category` (
`category_id` int(10) NOT NULL AUTO_INCREMENT,
`category_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`category_description` text NOT NULL,
`category_Url` varchar(255) NOT NULL,
`parent_category_id` int(11) DEFAULT NULL,
`store_id` varchar(250) DEFAULT NULL,
`meta_title` text,
`meta_keywords` text,
`meta_description` text,
`creation_date` datetime DEFAULT NULL,
`last_updated_date` datetime DEFAULT NULL,
`status` smallint(6) DEFAULT NULL,
PRIMARY KEY (`category_id`),
KEY `FK_Question_category_parent_category` (`parent_category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8
我如何像上面一样显示
如果要显示n级的父级和子级,请尝试以下代码。就我的观点而言,您希望显示父级和子级至n级的类别。首先选择所有父ID为0的根猫,并将其ID传递给此递归函数
function getChildCats($catId)
{
$sql = "select * from categories where parent_category_id = $cateID";
$res = mysql_query($sql);
$raws[];
while($raw = mysql_fetch_assoc($res))
{
$raw['sub'] = getChildCats($raw['category_id'])
$raws[] = $raw;
}
return $raws;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句