Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * Description of 3
- *
- * @author gunawan
- */
- class menu {
- //put your code here
- private $menu;
- function __CONSTRUCT(){
- $menu = array(
- 'top' => array(
- array(6, 'one menu'),
- array(1, 'multi menu')
- )
- );
- $menu['sub'] = array(
- '1' => array(
- array(2, 'one menu'),
- array(11, 'multi menu')
- )
- );
- $menu['sub'][11]=array(
- array(3, 'one menu'),
- array(4, 'multi menu')
- );
- $this->menu=$menu;
- }
- function get(){
- return $this->menu;
- }
- function run(){
- return $this->get_parent();
- }
- //=============FOKUS pada ini saja
- function get_parent(){//get_list
- /*
- $this->db->where('parent', "");
- $temp = $this->db->get('menu_navigation');
- $array = null;
- if($temp->num_rows() > 0){
- foreach ($temp->result_object() as $row) {
- if(($child = $this->get_child($row->id)) != null){
- $row->child = $child;
- }
- $array[] = $row;
- }
- }
- *
- * pada dasarnya sama seperti di atas.. hanya agak penyesuaian
- */
- $data= $this->menu['top'];//or parent
- $raw=array();
- foreach($data as $row){
- $tmp = array('id'=>$row[0], 'text'=>$row[1]); //mending langsung dibuat gini walau dari query udah melakukannya
- $have_child=$this->have_child($row[0]);
- if($have_child){
- $tmp['child']=$this->get_child($row[0]);
- }
- $raw[]=$tmp;
- }
- return $raw;
- }
- function have_child($id){
- $child=$this->menu['sub'];
- return isset($child[$id])?TRUE:FALSE;
- }
- function get_child($id){
- $data_child= $this->menu['sub'];//or child
- $data=$data_child[$id]; //asumsikan kamu dapatkan dari query where parent=xxx
- $raw=array();
- foreach($data as $row){
- $tmp = array('id'=>$row[0], 'text'=>$row[1]); //mending langsung dibuat gini walau dari query udah melakukannya
- $have_child=$this->have_child($row[0]);
- if($have_child){
- $tmp['child']=$this->get_child($row[0]);
- }
- $raw[]=$tmp;
- }
- return $raw;
- }
- }
- $menu = new menu();
- $ar = $menu->run();
- echo '<pre>'.print_r($ar,1). json_encode($ar);
- /**** HASIL
- Array
- (
- [0] => Array
- (
- [id] => 6
- [text] => one menu
- )
- [1] => Array
- (
- [id] => 1
- [text] => multi menu
- [child] => Array
- (
- [0] => Array
- (
- [id] => 2
- [text] => one menu
- )
- [1] => Array
- (
- [id] => 11
- [text] => multi menu
- [child] => Array
- (
- [0] => Array
- (
- [id] => 3
- [text] => one menu
- )
- [1] => Array
- (
- [id] => 4
- [text] => multi menu
- )
- )
- )
- )
- )
- )
- jsonnya:
- [
- {
- "id":6,
- "text":"one menu"
- },
- {
- "id":1,
- "text":"multi menu",
- "child":[
- {
- "id":2,
- "text":"one menu"
- },
- {
- "id":11,
- "text":"multi menu",
- "child":[
- {
- "id":3,
- "text":"one menu"
- },
- {
- "id":4,
- "text":"multi menu"
- }
- ]
- }
- ]
- }
- ]
- ****/
- /*
- function get_list(){
- $this->db->where('parent', "");
- $temp = $this->db->get('menu_navigation');
- $array = null;
- if($temp->num_rows() > 0){
- foreach ($temp->result_object() as $row) {
- if(($child = $this->get_child($row->id)) != null){
- $row->child = $child;
- }
- $array[] = $row;
- }
- }
- return $array;
- }
- function get_child($id){
- $this->db->where('parent', $id);
- $temp = $this->db->get('menu_navigation');
- $array = null;
- if($temp->num_rows() > 0){
- foreach ($temp->result_object() as $row) {
- $array[] = $row;
- }
- }
- return $array;
- }
- */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement