multi level menu with codeigniter - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: multi level menu with codeigniter (/showthread.php?tid=62432) |
multi level menu with codeigniter - codeigniter1932 - 07-14-2015 I want to create a multi level menu with codeIgniter. My table structure is this: id -- category -- parent_id I use this code in my model: function all_category_ordered($level = 0, $prefix = '<li>') { $rows = $this->db ->select('id,category,parent_id') ->where('parent_id', $level) ->order_by('id','asc') ->get('category') ->result(); $category = null; if (count($rows) > 0) { foreach ($rows as $row) { $category .= $prefix . $row->category . "\n"; // Append subcategories $category .= "</li>".$this->all_category_ordered($row->id, $prefix . '*') ; } } return $category; } it works fine and the out put of the code is like this: <li>menu 1</li> <li>menu 2</li> <li>menu 3</li> <li>*submenu 1</li> <li>menu 4</li> <li>*submenu 2</li> but I want to change the model to out put in ul and li correct order to style it correctly. preferred order: <ul> <li>menu 1</li> <li>menu 2</li> <li>menu 3 <ul> <li>*submenu 1</li> </ul> </li> <li>menu 3 <ul> <li>*submenu 1</li> </ul> </li> </ul> Any help would be appreciated. RE: multi level menu with codeigniter - InsiteFX - 07-14-2015 Read this it may help you. Sitepoint - Storing Hierarchical Data in a Database RE: multi level menu with codeigniter - mwhitney - 07-14-2015 I would recommend taking the HTML/formatting out of the model as a starting point. If you can create a library/helper with a method/function which simply takes an array and returns a formatted string for output, it should simplify the problem for you. Then your model can retrieve the data from the database and return a multidimensional array, e.g. Code: array( RE: multi level menu with codeigniter - davicotico - 07-14-2015 I have a post about that: http://davicotico.com/menu-multinivel-php-mysql-bootstrap/ It is in Spanish. Basically: 1.- Create a recursive function in a helper PHP Code: function create_menu(array $arrayItem, $id_parent = 0, $level = 0){ PHP Code: $sql = 'SELECT * FROM _menuitem where id_menu=? and _status=1 ORDER BY parent ASC'; RE: multi level menu with codeigniter - michalsn - 07-15-2015 Simple implementation with adjacency list: https://github.com/michalsn/CodeIgniter-Adjacency-List RE: multi level menu with codeigniter - Athov - 07-15-2015 this is my class PHP Code: <?php i have the 'Multi_categories' library autoloaded but you can do $this->load->library('Multi_categories'); PHP Code: public function categories_menu($category_id = 0) { PHP Code: $data['sidebar'] = $this->categories_menu($category); and in the view Code: <ul> <ul class="submenu"></ul> but it's a easy fix if you want to get only this main categories you can do PHP Code: $this->multi_categories->menu(0, ['stop' => true, 'level' => 0, 'active' => false]); main category>sub category to get all categories do PHP Code: $this->multi_categories->menu(0, ['stop' => false]); like main category> sub category> sub category do PHP Code: $this->multi_categories->menu(0, ['stop' => true, 'level' => 1]); level 2 = mc>sc>sc level 3 = mc>sc>sc>sc level * = mc>(level * sc) you can see the defaults here PHP Code: $stop = (( ! isset($data['stop'])) ? TRUE : $data['stop']); RE: multi level menu with codeigniter - skunkbad - 07-16-2015 The older version of Community Auth had an example of outputting a category menu where the elements were stored in the database in a parent -> child relationship. The example also accommodates categories where the names may be the same, and is fully and infinitely recursive: https://bitbucket.org/skunkbad/community-auth-git-version/src/032a2c579cb0089eea44bce3aa6a4b8ce1220fb6/application/controllers/category_menu.php?at=master RE: multi level menu with codeigniter - hamed.hossani - 02-21-2017 hi Multi level category multiselect CodeIgniter https://github.com/hamedhossani/Multi_level_category_multiselect_CodeIgniter |