Ok, kita langsung saja siapkan table untuk media penymipanan menu. Berikut struktrnya :
DROP TABLE IF EXISTS `menus`;
CREATE TABLE `menus` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`parent_id` int(11) NOT NULL DEFAULT '0',
`module` varchar(255) DEFAULT NULL,
`url` varchar(255) DEFAULT NULL,
`type` varchar(255) DEFAULT 'front' COMMENT 'front, backend',
`active` int(2) NOT NULL DEFAULT '1',
`order` int(2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
Selanjutnya berikut script/code library Tree Menu :
'; //parent tag open
private $pt_close = ''; //parent tag close
private $pt_slink = ''; //parent tag link
private $pt_attr = ''; /parent attribute
private $lpt_open = '
private $lpt_close = '
'; //list parent tag close
/*child properties*/
private $ct_open = '
- '; //child tag open private $ct_close = '
private $ct_attr = ''; //child tag attribute
private $lct_open = '
private $lct_close = '
'; //list child tag close
var $obarray, $list;
public function __construct($properties = array())
{
empty($properties) OR $this->initialize($properties, FALSE);
$this->_mimes =& get_mimes();
$this->_CI =& get_instance();
log_message('info', 'Menu Class Initialized');
}
public function initialize(array $properties = array()){
foreach ($properties as $key => $val)
{
if (property_exists($this, $key))
{
$this->$key = $val;
}
}
}
function buildTree($menu_array)
{
global $obarray, $list;
$list = $this->pt_open;
if (!is_array($menu_array)) return '';
$obarray = $menu_array;
foreach($obarray as $item){
if($item['parent'] == 0){
$mainlist = $this->_buildElements($item, 0);
}
}
$list .= $this->pt_close;
return $list;
}
private function _buildElements($parent, $append)
{
global $obarray, $list;
if($parent['module']!='pages'){
if($this->_hasChild($parent['id'])){
$list .= $this->lpt_open . anchor($parent['module'], $parent['name'].$this->pt_attr, $this->pt_slink);
}else{
$list .= $this->lct_open . anchor($parent['module'], $parent['name'], '') . $this->lct_close;
}
}else{
if($this->_hasChild($parent['id'])){
$list .= $this->lpt_open . anchor($parent['module'].'/'.$parent['url'], $parent['name'].$this->pt_attr, $this->pt_slink);
}else{
$list .= $this->lct_open . anchor($parent['module'].'/'.$parent['url'], $parent['name'], '') . $this->lct_close;
}
}
if($this->_hasChild($parent['id'])){
$append++;
$list .= $this->ct_open;
$child = $this->_buildArray($parent['id']);
foreach($child as $item){
$list .= $this->_buildElements($item, $append);
}
if($this->_hasChild($parent['id'])){
$list .= $this->lpt_close;
}
$list .= $this->ct_close;
}
}
private function _hasChild($parent)
{
global $obarray;
$counter = 0;
foreach($obarray as $item){
if($item['parent'] == $parent){
++$counter;
}
}
return $counter;
}
private function _buildArray($parent)
{
global $obarray;
$bArray = array();
foreach($obarray as $item){
if($item['parent'] == $parent){
array_push($bArray, $item);
}
}
return $bArray;
}
}
Cara menggunakan sebagai berikut:
load->library(‘omenu’);
}
Public function index()
{
$data[‘menu’] = $this->get_menus();
$this->load->view(‘halama_utama’,$data);
}
Public function get_menus()
{
$properties = array(
'pt_open' => '
',
'pt_slink' => 'class="dropdown-toggle" data-toggle="dropdown"',
'pt_attr' => ' ',
'lpt_open' => '
'lpt_close' => '
',
'ct_open' => '
',
'ct_attr' => '',
'lct_open' => '
'lct_close' => '
'
);
$this->load->library('omenu', $properties);
$this->omenu->initialize($properties);
$this->db->where('active', 1);
$menu = $this->db->get('menus')->result_array();
return $this->omenu->buildTree($menu);
}
}
Lalu kita buat view,
<….. tag html….>
<….. tag html….>
Mudah bukan… semoga bermaafat. Sampai jumpa pada artikel berikutnya.