ram4nd Posted February 16, 2010 Share Posted February 16, 2010 The condition doesn't work, it does the cycle only once, but it has to make it twice: static $last, $now; while($last == $now) { $last = $now; foreach($tutorials_from_categories as $tutorial) { $tutorials_from_categories = array_merge($tutorials_from_categories, $this->model->get_categories_by_parent($tutorial)); } $tutorials_from_categories = array_unique($tutorials_from_categories); $now = count($tutorials_from_categories); } My model: function get_categories_by_parent($parent_id) { static $return; $this->db->select('id'); $this->db->where('parent_id', $parent_id); $query = $this->db->get('categories'); if($query->num_rows()>0) { foreach($query->result() as $row) { $return[] = $row->id; } return $return; } else return array(); } I can also post part of mysql table, but I think it's not necessary. Quote Link to comment Share on other sites More sharing options...
ram4nd Posted February 16, 2010 Author Share Posted February 16, 2010 I tried 2 foreach loops in a row. Doesn't ever end the loop, so i think my while is ok, there is some other mistake Quote Link to comment Share on other sites More sharing options...
ram4nd Posted February 16, 2010 Author Share Posted February 16, 2010 Ok i think i almost have solved the problem. The problem is that i have used $tutorials_from_categories everywhere, but i cant see solution just quite yet Quote Link to comment Share on other sites More sharing options...
khr2003 Posted February 17, 2010 Share Posted February 17, 2010 what are you trying to achieve here? what is the end result will be? Quote Link to comment Share on other sites More sharing options...
ram4nd Posted February 17, 2010 Author Share Posted February 17, 2010 I want to generate array of categories, where I have base category and then sub categories of that base category, and the system should work with any amount of subcategories. Quote Link to comment Share on other sites More sharing options...
ram4nd Posted February 17, 2010 Author Share Posted February 17, 2010 Works like this, I don't understand the while command: $tutorials_from_categories[0] = $category; static $last, $now, $temp; while(1) { $last = $now; $temp = $tutorials_from_categories; foreach($temp as $tutorial) { $tutorials_from_categories = array_merge($tutorials_from_categories, $this->model->get_categories_by_parent($tutorial)); } $tutorials_from_categories = array_unique($tutorials_from_categories); $now = count($tutorials_from_categories); if($last == $now) break; } Quote Link to comment 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.