I am working on business directory sort of thing, and need to show recursive parents of categories at category listing.
I am using following function for that:
public function get_recursive_parents($category_id){
$categories = array();
$res = $this->db->from('categories')->where('cat_id',$category_id)->get()->row_array();
$cat_id = $res['parent_id'];
$categories[] = $res;
while($cat_id){
$res = $this->db->from('categories')->where('cat_id',$cat_id)->get()->row_array();
$categories[] = $res;
$cat_id = $res['parent_id'];
}
return $categories;
}
I am using this function and as it is on admin site and a bit slow at admin site can is fine too, and admin will be only one so I can give it more memory.But I think limit memory more than 300M for one call is too much and still getting this:
Fatal error: Allowed memory size of 367001600 bytes exhausted (tried to allocate 72 bytes) in /var/www/usmanproject/salesfinder/system/database/DB_active_rec.php on line 2007
So is there way so that I can optimize above function? or I need to do some specific sort of indexing or algo optimization, or any other possible way? Or I just stop showing all parents and super parents of category (that is client demand to see hierarchy)? Or need to increase memory as I already worked on a directory and that was also slow at admin site so I guess they were just using more Memory?
Any advice will be appreciated.
Here is that table schema, it has parent_id so it is working as recursive relation.
CREATE TABLE IF NOT EXISTS `categories` (
`cat_id` int(11) NOT NULL AUTO_INCREMENT,
`cat_name` varchar(255) DEFAULT NULL,
`cat_title` varchar(255) DEFAULT NULL,
`cat_desc` varchar(255) DEFAULT NULL,
`cat_text` text,
`parent_id` int(11) NOT NULL,
`cat_img` varchar(255) DEFAULT NULL,
`sort_id` int(11) NOT NULL DEFAULT '1',
`last_level` tinyint(4) NOT NULL,
PRIMARY KEY (`cat_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=221 ;