ram4nd Posted April 6, 2010 Share Posted April 6, 2010 Hi, I have table with categories can be unlimited amount of subcategories, all in 1 table using parent_id. I have items that belong to categories. I need to count items in given category and sub categories. How can i do it and what is the most simple way? Quote Link to comment https://forums.phpfreaks.com/topic/197758-recursive-function-and-arrays/ Share on other sites More sharing options...
maca134 Posted April 6, 2010 Share Posted April 6, 2010 Hey assuming you have an open database connection, the code below might be suitable: class recursive_scan { public $item_count = 0; function scan( $parent_cat_id = 0 ) { $result = mysql_query('SELECT * FROM `cats` WHERE `parent_id` = "' . $parent_cat_id . '"'); if ( mysql_num_rows($result) > 0 ) { $item_count = $item_count + mysql_num_rows($result); while ( $row = mysql_fetch_assoc($result) ) { $this->scan( $row['id'] ); } } } function get_item_count() { return $this->item_count; } } $rs = new recursive_scan; $rs->scan(); var_dump( $rs->get_item_count() ); I have tested it but it should work Quote Link to comment https://forums.phpfreaks.com/topic/197758-recursive-function-and-arrays/#findComment-1037822 Share on other sites More sharing options...
shlumph Posted April 6, 2010 Share Posted April 6, 2010 I'd do it with just one query, no need for recursion: SELECT COUNT(id) FROM categories WHERE parent_cat_id = '{$cat_id}' OR cat_id = '{$cat_id}' Quote Link to comment https://forums.phpfreaks.com/topic/197758-recursive-function-and-arrays/#findComment-1037827 Share on other sites More sharing options...
ram4nd Posted April 6, 2010 Author Share Posted April 6, 2010 Hey assuming you have an open database connection, the code below might be suitable: class recursive_scan { public $item_count = 0; function scan( $parent_cat_id = 0 ) { $result = mysql_query('SELECT * FROM `cats` WHERE `parent_id` = "' . $parent_cat_id . '"'); if ( mysql_num_rows($result) > 0 ) { $item_count = $item_count + mysql_num_rows($result); while ( $row = mysql_fetch_assoc($result) ) { $this->scan( $row['id'] ); } } } function get_item_count() { return $this->item_count; } } $rs = new recursive_scan; $rs->scan(); var_dump( $rs->get_item_count() ); I have tested it but it should work Thanks, didn't think about putting it into class. I use codeigniter, i'll put the sql and other stuff like they have to be myself. I'd do it with just one query, no need for recursion: SELECT COUNT(id) FROM categories WHERE parent_cat_id = '{$cat_id}' OR cat_id = '{$cat_id}' Sure you do, I have a dynamic thing you know. Read my post fully and you'll understand. Quote Link to comment https://forums.phpfreaks.com/topic/197758-recursive-function-and-arrays/#findComment-1037844 Share on other sites More sharing options...
maca134 Posted April 6, 2010 Share Posted April 6, 2010 Oh i use codeigniter all the time... Its easy to integrate it into a controller class Home extends Controller { public $item_count = 0; ...your other vars/controllers/etc... function _scan( $parent_cat_id = 0 ) { $result = mysql_query('SELECT * FROM `cats` WHERE `parent_id` = "' . $parent_cat_id . '"'); if ( mysql_num_rows($result) > 0 ) { $item_count = $item_count + mysql_num_rows($result); while ( $row = mysql_fetch_assoc($result) ) { $this->_scan( $row['id'] ); } } } function _get_item_count() { return $this->item_count; } } Or you could put the function into a Model, which would make sense, as they are for DB stuff Quote Link to comment https://forums.phpfreaks.com/topic/197758-recursive-function-and-arrays/#findComment-1037849 Share on other sites More sharing options...
shlumph Posted April 6, 2010 Share Posted April 6, 2010 I misunderstood the wording of unlimited amount of subcategories. If sub categories can have sub categories, and those sub-sub categories can have sub categories, etc. then surely, you'll need a recursive function. Quote Link to comment https://forums.phpfreaks.com/topic/197758-recursive-function-and-arrays/#findComment-1037858 Share on other sites More sharing options...
ram4nd Posted April 6, 2010 Author Share Posted April 6, 2010 Oh i use codeigniter all the time... Its easy to integrate it into a controller class Home extends Controller { public $item_count = 0; ...your other vars/controllers/etc... function _scan( $parent_cat_id = 0 ) { $result = mysql_query('SELECT * FROM `cats` WHERE `parent_id` = "' . $parent_cat_id . '"'); if ( mysql_num_rows($result) > 0 ) { $item_count = $item_count + mysql_num_rows($result); while ( $row = mysql_fetch_assoc($result) ) { $this->_scan( $row['id'] ); } } } function _get_item_count() { return $this->item_count; } } Or you could put the function into a Model, which would make sense, as they are for DB stuff Yes sure I understand all that, just I am new with recursions. I misunderstood the wording of unlimited amount of subcategories. If sub categories can have sub categories, and those sub-sub categories can have sub categories, etc. then surely, you'll need a recursive function. Good that you get it now. Quote Link to comment https://forums.phpfreaks.com/topic/197758-recursive-function-and-arrays/#findComment-1037868 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.