项目作者: jdriesen

项目描述 :
Infinite-deep MultiLevel Menu in CodeIgniter
高级语言: PHP
项目地址: git://github.com/jdriesen/MultiLevelMenu.git
创建时间: 2017-03-24T09:23:58Z
项目社区:https://github.com/jdriesen/MultiLevelMenu

开源协议:MIT License

下载


Infinite-deep MultiLevel Menu in CodeIgniter

A small CodeIgniter Controller and Model to generate an (in theory… infinite-deep) multi-level menu.

Included features

  • As less database calls as possible (we’ll need only 1)
  • As generic as possible (using a recursive function)
  • As clean as possible
  • The menu in this example is 5 levels ‘deep’ (friends, it can be as deep as the ocean…)
  • following the HMVC specs.

Sourcecode contains only 25 relevant lines

The result will be something like this

Result MultiLevel Menu in CodeIgniter

Requirement

  • CodeIgniter 3.0.x (tested until 3.1.3) installed.
  • HMVC installed
  • Some basic PHP knowledge
  • Some basic MySQL knowledge

Step 1: creating the table in MySQL.

Our table (named ‘menus’) will have only 4 fields…

  • id (int) : Unique AI integer
  • parent_id (int) : refers to the id…
  • name (varchar) : the name of our menu
  • sequence (int) : will determine the ‘order’ in which the menu-items will be shown (per level …)

Remark:
By using the sequence field, we can make to menu-levels independent from the ID’s in our table…

See the image below for details

Rec Id in your database does not really mather

The SQL script to create, and fill your ‘menus‘ table is included in the SQL folder

Step 2: The Controller.

  1. class Menus extends MY_Controller {
  2. function __construct() {
  3. parent::__construct();
  4. $this -> load -> model('menus/Mdl_menus');
  5. }
  6. function index() {
  7. $items = $this->Mdl_menus->get_items();
  8. $menu = $this->Mdl_menus->generateTree($items);
  9. $data = array(
  10. 'menu' => $menu,
  11. );
  12. $this->load->view('menus/menu', $data, false);
  13. }
  14. }

Pretty simple, no ?

Step 3: The Model.

Beside the constructor, our Model has 2 functions…

  • get_items() does 1 database call (yep, only ONE)
  • generateTree() is a recursive function which does the complete job. In other words, it generates ALL the menu levels, in a nice and clean way…
  1. class Mdl_menus extends CI_Model {
  2. function __construct() {
  3. parent::__construct();
  4. $this->tableName = 'menus';
  5. }
  6. function get_items() {
  7. $this->db->select('*');
  8. $this->db->from($this->tableName);
  9. $this->db->order_by('parent_id');
  10. $this->db->order_by('sequence');
  11. $query = $this->db->get();
  12. return $query->result_array();
  13. }
  14. function generateTree($items = array(), $parent_id = 0){
  15. $tree = '<ul>';
  16. for($i=0, $ni=count($items); $i < $ni; $i++){
  17. if($items[$i]['parent_id'] == $parent_id){
  18. $tree .= '<li>';
  19. $tree .= $items[$i]['name'];
  20. $tree .= $this->generateTree($items, $items[$i]['id']);
  21. $tree .= '</li>';
  22. }
  23. }
  24. $tree .= '</ul>';
  25. return $tree;
  26. }
  27. } // End of Model Class

Step 4 The view

Basic rule of HMVC … NO Business Logic in your views…

Well, this is the content of our View …

  1. echo $menu;

And we are DONE !!!

Once the above files are copied to the right location, you can simply run this small demo by:

http\\ yourserver \ yourappname \ menus

Enjoy & Grtz,

Johnny