multi level menu with codeigniter |
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.
Read this it may help you.
Sitepoint - Storing Hierarchical Data in a Database What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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(
07-14-2015, 10:03 AM
(This post was last modified: 07-14-2015, 10:11 AM by davicotico. Edit Reason: Detail )
I have a post about that:
http://davicotico.com/menu-multinivel-ph...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';
Simple implementation with adjacency list: https://github.com/michalsn/CodeIgniter-Adjacency-List
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']);
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...?at=master
hi
Multi level category multiselect CodeIgniter https://github.com/hamedhossani/Multi_le...odeIgniter |
Welcome Guest, Not a member yet? Register Sign In |