Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save syaiful-scooter/3ac16ca1c9cb17b2b4c3d709b7b9a3b8 to your computer and use it in GitHub Desktop.
Save syaiful-scooter/3ac16ca1c9cb17b2b4c3d709b7b9a3b8 to your computer and use it in GitHub Desktop.
sample-menu-generator
<?php
class Menu extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Menu_model');
}
public function index()
{
$this->load->view('Menu');
}
public function getMenu($parent = 0)
{
$menu = $this->Menu_model->fetchMenu($parent);
$ul = '<ul>';
foreach($menu as $key => $item) {
// parent menu
$ul .= '<li><a href="'.$item['url'].'">'.$item['menu_name'].'</a></li>';
$child = $this->Menu_model->fetchMenu($item['menu_id']);
if ($child) {
$ul .= '<ul>';
//$menu[$key]['child'] = $child;
foreach ($child as $key2 => $ch) {
// anak pertama
$ul .= '<li><a href="'.$ch['url'].'">'.$ch['menu_name'].'</a></li>';
$cucu = $this->Menu_model->fetchMenu($ch['menu_id']);
if ($cucu) {
$ul .= '<ul>';
foreach($cucu as $key3 => $cc) {
$ul .= '<li><a href="'.$cc['url'].'">'.$cc['menu_name'].'</a></li>';
}
$ul .= '</ul>';
}
}
$ul .= '</ul>';
}
}
$ul .= '</ul>';
//echo ($ul); exit;
$data = [
'status' => true,
'menu' => $ul
];
echo json_encode($data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment