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. Link to comment https://forums.phpfreaks.com/topic/192260-while-loop-question/ 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 Link to comment https://forums.phpfreaks.com/topic/192260-while-loop-question/#findComment-1013163 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 Link to comment https://forums.phpfreaks.com/topic/192260-while-loop-question/#findComment-1013170 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? Link to comment https://forums.phpfreaks.com/topic/192260-while-loop-question/#findComment-1013540 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. Link to comment https://forums.phpfreaks.com/topic/192260-while-loop-question/#findComment-1013567 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; } Link to comment https://forums.phpfreaks.com/topic/192260-while-loop-question/#findComment-1013571 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.