Search code examples
magento

how to display category and sub category in magento


I have created custom module for magento the problem in table which consists of category and subcategory now I want to display the following

Category 1
Subcategory 1
Subcategory 2
Subcategory 3
Category 2
Subcategory 1
Subcategory 2
Subcategory 3
Category 3
Subcategory 1
Subcategory 2
Subcategory 3 

my code is

$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>';
          
           
        }
     }

our table structure is

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  

                         

How I can display like the above


Solution

  • Try this code if you want to show child and parent with n levels.As for i get your point you want to show categories with parent and child upto n-levels. First select all your root cats with parent id 0 and pass their ids to this recursive function

    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;
        }