0

我已经为 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  

                         

我如何像上面那样显示

4

1 回答 1

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;
    }
于 2013-08-03T05:32:00.707 回答