Advertisement
kura2yamato

begini lho hasilnya

Nov 3rd, 2017
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.27 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9.  
  10. /**
  11.  * Description of 3
  12.  *
  13.  * @author gunawan
  14.  */
  15. class menu {
  16.     //put your code here
  17.     private $menu;
  18.     function __CONSTRUCT(){
  19.         $menu = array(
  20.             'top' => array(
  21.                 array(6, 'one menu'),
  22.                 array(1, 'multi menu')
  23.             )
  24.         );
  25.  
  26.         $menu['sub'] = array(
  27.             '1' => array(
  28.                 array(2, 'one menu'),
  29.                 array(11, 'multi menu')
  30.             )
  31.         );
  32.         $menu['sub'][11]=array(
  33.                 array(3, 'one menu'),
  34.                 array(4, 'multi menu')
  35.             );
  36.         $this->menu=$menu;
  37.     }
  38.    
  39.     function get(){
  40.         return $this->menu;
  41.     }
  42.    
  43.     function run(){
  44.        
  45.         return $this->get_parent();
  46.     }
  47.     //=============FOKUS pada ini saja
  48.     function get_parent(){//get_list
  49.         /*
  50.         $this->db->where('parent', "");
  51.         $temp = $this->db->get('menu_navigation');
  52.         $array = null;
  53.         if($temp->num_rows() > 0){
  54.             foreach ($temp->result_object() as $row) {
  55.                 if(($child = $this->get_child($row->id)) != null){
  56.                     $row->child = $child;
  57.                 }
  58.                 $array[] = $row;
  59.             }
  60.         }
  61.          *
  62.          * pada dasarnya sama seperti di atas.. hanya agak penyesuaian
  63.         */
  64.         $data= $this->menu['top'];//or parent
  65.        
  66.         $raw=array();
  67.         foreach($data as $row){
  68.             $tmp = array('id'=>$row[0], 'text'=>$row[1]); //mending langsung dibuat gini walau dari query udah melakukannya
  69.             $have_child=$this->have_child($row[0]);
  70.             if($have_child){
  71.                 $tmp['child']=$this->get_child($row[0]);
  72.             }
  73.             $raw[]=$tmp;
  74.         }
  75.         return $raw;
  76.     }
  77.    
  78.     function have_child($id){
  79.         $child=$this->menu['sub'];
  80.         return isset($child[$id])?TRUE:FALSE;
  81.     }
  82.    
  83.     function get_child($id){
  84.         $data_child= $this->menu['sub'];//or child
  85.         $data=$data_child[$id]; //asumsikan kamu dapatkan dari query where parent=xxx
  86.         $raw=array();
  87.         foreach($data as $row){
  88.             $tmp = array('id'=>$row[0], 'text'=>$row[1]); //mending langsung dibuat gini walau dari query udah melakukannya
  89.             $have_child=$this->have_child($row[0]);
  90.             if($have_child){
  91.                 $tmp['child']=$this->get_child($row[0]);
  92.             }
  93.             $raw[]=$tmp;
  94.         }
  95.         return $raw;
  96.     }
  97. }
  98.  
  99. $menu = new menu();
  100.  
  101. $ar = $menu->run();
  102.  
  103. echo '<pre>'.print_r($ar,1).  json_encode($ar);
  104. /**** HASIL
  105. Array
  106. (
  107.     [0] => Array
  108.         (
  109.             [id] => 6
  110.             [text] => one menu
  111.         )
  112.  
  113.     [1] => Array
  114.         (
  115.             [id] => 1
  116.             [text] => multi menu
  117.             [child] => Array
  118.                 (
  119.                     [0] => Array
  120.                         (
  121.                             [id] => 2
  122.                             [text] => one menu
  123.                         )
  124.  
  125.                     [1] => Array
  126.                         (
  127.                             [id] => 11
  128.                             [text] => multi menu
  129.                             [child] => Array
  130.                                 (
  131.                                     [0] => Array
  132.                                         (
  133.                                             [id] => 3
  134.                                             [text] => one menu
  135.                                         )
  136.  
  137.                                     [1] => Array
  138.                                         (
  139.                                             [id] => 4
  140.                                             [text] => multi menu
  141.                                         )
  142.  
  143.                                 )
  144.  
  145.                         )
  146.  
  147.                 )
  148.  
  149.         )
  150.  
  151. )
  152.  
  153. jsonnya:
  154. [
  155.  
  156.     {
  157.         "id":6,
  158.         "text":"one menu"
  159.     },
  160.     {
  161.         "id":1,
  162.         "text":"multi menu",
  163.         "child":[
  164.             {
  165.                 "id":2,
  166.                 "text":"one menu"
  167.             },
  168.             {
  169.                 "id":11,
  170.                 "text":"multi menu",
  171.                 "child":[
  172.                     {
  173.                         "id":3,
  174.                         "text":"one menu"
  175.                     },
  176.                     {
  177.                         "id":4,
  178.                         "text":"multi menu"
  179.                     }
  180.                 ]
  181.             }
  182.         ]
  183.     }
  184.  
  185. ]
  186.  
  187.  
  188.  
  189. ****/
  190. /*     
  191.     function get_list(){
  192.         $this->db->where('parent', "");
  193.         $temp = $this->db->get('menu_navigation');
  194.         $array = null;
  195.         if($temp->num_rows() > 0){
  196.             foreach ($temp->result_object() as $row) {
  197.                 if(($child = $this->get_child($row->id)) != null){
  198.                     $row->child = $child;
  199.                 }
  200.                 $array[] = $row;
  201.             }
  202.         }
  203.         return $array;
  204.     }
  205.  
  206.     function get_child($id){
  207.         $this->db->where('parent', $id);
  208.         $temp = $this->db->get('menu_navigation');
  209.         $array = null;
  210.         if($temp->num_rows() > 0){
  211.             foreach ($temp->result_object() as $row) {
  212.                 $array[] = $row;
  213.             }
  214.         }
  215.         return $array;
  216.     }
  217.     */
  218.  
  219. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement