Membuat Menu Dinamis Codeigniter + MySql

Membuat menu dinamis dibutuhkan apabila kita akan membuat sebuah aplikasi multi user, dimana hak akses menu-menu setiap user berbeda antara beberapa atau satu dan yang lainnya. Berikut ini cara paling mudah untuk mengimplementasikan menu dinamis.

Tools yang digunakan adalah:

  1. Database Mysql
  2. Local webserver (Apache dll)
  3. Php framework Codeigniter
  4. Text editor (Dreamweaver dll)

STRUKTUR DATABASE

Create table sys_menu

CREATE TABLE `sys_menu` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL DEFAULT ”,
`link_type` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT ‘uri’,
`page_id` int(11) NOT NULL DEFAULT ‘0’,
`module_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT ”,
`url` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT ”,
`uri` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT ”,
`dyn_group_id` int(11) NOT NULL DEFAULT ‘0’,
`position` int(5) NOT NULL DEFAULT ‘0’,
`target` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`parent_id` int(11) NOT NULL DEFAULT ‘0’,
`is_parent` tinyint(1) NOT NULL DEFAULT ‘0’,
`show_menu` enum(‘0′,’1’) COLLATE utf8_unicode_ci NOT NULL DEFAULT ‘1’,
`fa_icon` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `dyn_group_id – normal` (`dyn_group_id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Insert into table

INSERT INTO `_tmp_sys_menu` (`id`, `title`, `link_type`, `page_id`, `module_name`, `url`, `uri`, `dyn_group_id`, `position`, `target`, `parent_id`, `is_parent`, `show_menu`, `fa_icon`) VALUES
(1,’Category 1′,’page’,1,”,’http://www.category1.com’,”,1,0,”,0,1,’1′,NULL),
(2,’Category 1 – 1′,’page’,2,”,’http://www.category11.com’,”,1,0,”,1,0,’1′,NULL),
(3,’Category 1 – 2′,’page’,3,”,’http://www.category12.com’,”,1,0,”,1,1,’1′,NULL),
(4,’Category 1 – 2 – 1′,’page’,4,”,’http://www.category121.com’,”,1,0,”,3,0,’1′,NULL),
(5,’Category 1 – 2 – 2′,’page’,5,”,’http://www.category122.com’,”,1,0,”,3,1,’1′,NULL),
(6,’Category 1 – 2 – 2 – 1′,’page’,6,”,’http://www.category1221.com’,”,1,0,”,5,0,’1′,NULL),
(7,’Category 1 – 2 – 2 – 2′,’page’,7,”,’http://www.category1222.com’,”,1,0,”,5,0,’1′,NULL),
(8,’Category 2′,’page’,8,”,’http://www.category2.com’,”,1,0,”,0,0,’1′,NULL),
(9,’Category 3′,’page’,9,”,’http://www.category3.com’,”,1,0,”,0,1,’1′,NULL),
(10,’Category 3 – 1′,’page’,10,”,’http://www.category31.com’,”,1,0,”,9,0,’1′,NULL),
(11,’Category 3 – 2′,’page’,11,”,’http://www.category32.com’,”,1,0,”,9,0,’1′,NULL),
(12,’Category 3 – 3′,’page’,12,”,’http://www.category33.com’,”,1,0,”,9,0,’1′,NULL),
(13,’Category 3 – 4′,’page’,13,”,’http://www.category34.com’,”,1,0,”,9,0,’1′,NULL),
(14,’Category 3 – 5′,’page’,14,”,’http://www.category35.com’,”,1,0,”,9,0,’1′,NULL),
(15,’Category 3 – 6′,’page’,15,”,’http://www.category36.com’,”,1,0,”,9,0,’1′,NULL),
(16,’Category 4′,’page’,16,”,’http://www.category4.com’,”,1,0,”,0,0,’1′,NULL);

LIBRARY CODEIGNITER

Dalam tutorial ini sebuah file menu generator diberi nama Sitemap_menu.php disimpan dalam folder libraries

blog1

File Sitemap_menu.php

<?php
/*
* Dynmic_menu.php
*/
class Sitemap_menu {

private $ci; // para CodeIgniter Super Global Referencias o variables globales
private $id_menu = ‘id=”menu”‘;
private $class_menu = ‘class=”treeview-menu”‘;
private $class_parent = ‘class=”parent”‘;
private $class_last = ‘class=”last”‘;

function __construct(){
$this->ci =& get_instance(); // get a reference to CodeIgniter.
}
function build_menu($type){
$menu = array();
$query = $this->ci->db->query(“select * from sys_menu”);
switch ($type){
case 0: // 0 = top menu
break;
case 1: // 1 = horizontal menu
$html_out = “\t\t”.'<ul ‘.$this->class_menu.’>’.”\n”;
break;
case 2: // 2 = sidebar menu
$html_out = “\t\t”.'<ul ‘.$this->class_menu.’>’.”\n”;
break;
case 3: // 3 = footer menu
break;
default: // default = horizontal menu
$html_out .= “\t\t”.'<ul ‘.$this->class_menu.’>’.”\n”;
break;
}
foreach ($query->result() as $row){
$id = $row->id;
$title = $row->title;
$link_type = $row->link_type;
$page_id = $row->page_id;
$module_name = $row->module_name;
$url = $row->url;
$uri = $row->uri;
$dyn_group_id = $row->dyn_group_id;
$position = $row->position;
$target = $row->target;
$parent_id = $row->parent_id;
$is_parent = $row->is_parent;
$show_menu = $row->show_menu;
$fa_icon = $row->fa_icon;
if ($show_menu && $parent_id == 0){
if($is_parent == 1){
$html_out .= ‘<li>’.anchor($url, ‘<i class=”‘.$fa_icon.'”></i> ‘.$title.”<i class=’fa fa-angle-left pull-right’></i>”, ‘name=”‘.$title.'” id=”‘.$id.'” target=”‘.$target.'”‘);
$html_out .= $this->get_childs($id);
} else {
$html_out .= ‘<li>’.anchor($url, ‘<i class=”‘.$fa_icon.'”></i> ‘.$title, ‘name=”‘.$title.'” id=”‘.$id.'” target=”‘.$target.'”‘);
$html_out .= $this->get_childs($id);
}
}
}
$html_out .= ‘</li>’.”\n”;
$html_out .= “\t\t”.'</ul>’ . “\n”;
return $html_out;
}

function get_childs($id){
/*?> alert() <?php*/
$has_subcats = FALSE;
$html_out = ”;
$html_out .= “\t\t\t\t\t”.'<ul ‘.$this->class_menu.’>’.”\n”;
$query2 = $this->ci->db->query(“select * from sys_menu where parent_id = $id”);
if(($query2->num_rows()) > 0){
foreach ($query2->result() as $row){
$id = $row->id;
$title = $row->title;
$link_type = $row->link_type;
$page_id = $row->page_id;
$module_name = $row->module_name;
$url = $row->url;
$uri = $row->uri;
$dyn_group_id = $row->dyn_group_id;
$position = $row->position;
$target = $row->target;
$parent_id = $row->parent_id;
$is_parent = $row->is_parent;
$show_menu = $row->show_menu;
$fa_icon = $row->fa_icon;
$has_subcats = TRUE;
if ($is_parent == 1){
$html_out .= ‘<li>’.anchor($url, ‘<i class=”‘.$fa_icon.'”></i> ‘.$title.”<i class=’fa fa-angle-left pull-right’></i>”, ‘name=”‘.$title.'” id=”‘.$id.'” target=”‘.$target.'”‘);
}
else{
$html_out .= ‘<li>’.anchor($url, ‘<i class=”‘.$fa_icon.'”></i> ‘.$title, ‘name=”‘.$title.'” id=”‘.$id.'” target=”‘.$target.'”‘);
}
// Recurse call to get more child submenus.
$html_out .= $this->get_childs($id);
}
}
$html_out .= ‘</li>’ . “\n”;
$html_out .= “\t\t\t\t\t”.'</ul>’ . “\n”;
return ($has_subcats) ? $html_out : FALSE;
}
}
?>

Seting agar librari diatas otomatis di-load oleh Codeigniter. Buka file config/autoload.php

$autoload[‘libraries’] = array(‘database’,’sitemap_menu’);

Siapkan sebuah file bernama left.php di folder view

blog2

Panggil library Sitemap_menu.php dari view left.php

<?php echo $this->sitemap_menu->build_menu(‘2’); ?>

Potongan script diatas akan memerintahkan Codeigniter untuk memanggil library sitemap_menu dari file Sitemap_menu.php. build_menu adalah nama function yang terdapat dalam library.

HASIL MENU DINAMIS

blog3

Menu dinamis ini menggunakan perulangan rekrusif. Dengannya submenu yang dibuat bisa tak terhingga.

blog4

Demikian tutorialnya, semoga bisa berguna teman-teman. Terima kasih.

Advertisements

4 thoughts on “Membuat Menu Dinamis Codeigniter + MySql

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s