2013-08-02 41 views
0

類別和子類別i已經創建自定義模塊,其用於的Magento它由類別和子類別的表中的問題,現在我想顯示以下 第1類 子目錄1 子目錄2 子類別3 類別2 類別1 子目錄2 子目錄3 類別3 子目錄1 子目錄2 子目錄3如何顯示在Magento

我的代碼是 $集合=馬GE :: 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)字符集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默認字符集= UTF8

所以如何我可以顯示象上述 任何人的幫助。謝謝

+0

是什麼structure.Are您使用Magento的類別或自定義類別表? –

回答

0

試試這個代碼,如果你想顯示的孩子和家長有n levels.As我得到你的觀點,你想顯示類別與父母和子女高達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; 
    } 
+0

您的代碼非常感謝。 – chwajahat

+0

@ mahmood-rehman它可能會增強此代碼,以便爲應用程序顯示的每個n級別包含嵌套的下拉類別。我有很多類別,但我無法成功生成嵌套代碼。 brgds –