如何在magento中显示类别和子类别

时间:2013-08-02 10:09:49

标签: categories

我为magento创建了自定义模块表中的问题,包括类别和子类别现在我要显示以下内容 第1类 子类别1 子类别2 子类别3 第2类 子类别1 子类别2 子类别3 第3类 子类别1 子类别2 子类别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文本NOT NULL,
                        category_Url varchar(255)NOT NULL,
                        parent_category_id int(11)DEFAULT NULL,
                        store_id varchar(250)DEFAULT NULL,
                        meta_title文字,
                        meta_keywords文字,
                        meta_description文字,
                        creation_date日期时间DEFAULT NULL,
                        last_updated_date日期时间DEFAULT NULL,
                        status smallint(6)DEFAULT NULL,
                        PRIMARY KEY(category_id),
                        KEY FK_Question_category_parent_categoryparent_category_id
                      )ENGINE = InnoDB AUTO_INCREMENT = 81 DEFAULT CHARSET = utf8

所以我怎么能像上面那样展示 任何人的帮助。感谢

1 个答案:

答案 0 :(得分:0)

如果你想要显示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;
    }