suryakala

php

Feb 14th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.16 KB | None | 0 0
  1.                     <?php
  2.                     if(isset($_GET['input']))
  3.                     {
  4.                     $con=mysqli_connect("localhost","root","admin321","php");      
  5.  
  6.                     if (mysqli_connect_errno())                
  7.                       {
  8.                       echo "Failed to connect to MySQL: " . mysqli_connect_error();
  9.                       }
  10.                        $name=$_GET['input'];
  11.                      $sql="SELECT * FROM tab where value LIKE '%".$name."%'";
  12.                     $r = mysqli_query($con,$sql);
  13.                             $data = array();
  14.                             while($row = mysqli_fetch_assoc($r)) {
  15.                              $data[] = $row;
  16.                              }  
  17.                              function buildtree($src_arr, $parent_id = 0, $tree = array())
  18.                     {
  19.                         foreach($src_arr as $idx => $row)
  20.                         {
  21.                             if($row['parent'] == $parent_id)
  22.                             {
  23.                                 foreach($row as $k => $v)
  24.                                     $tree[$row['id']][$k] = $v;
  25.                                 unset($src_arr[$idx]);
  26.                                 $tree[$row['id']]['children'] = buildtree($src_arr, $row['id']);
  27.                             }
  28.                         }
  29.                         ksort($tree);
  30.                         return $tree;
  31.                     }
  32.  
  33.                     function insertIntoNestedArray(&$array, $searchItem){
  34.                      
  35.                         if($searchItem['parent'] == 0){
  36.                             array_push($array, $searchItem);
  37.                             return;
  38.                         }
  39.                         if(empty($array)){ return; }
  40.                      array_walk($array, function(&$item, $key, $searchItem){
  41.                         if($item['id'] == $searchItem['parent']){
  42.                                 array_push($item['children'], $searchItem);
  43.                                 return;
  44.                             }
  45.                             insertIntoNestedArray($item['children'], $searchItem);
  46.                     }, $searchItem);
  47.                     }
  48.                     $nestedArray = array();
  49.                     foreach($data as $itemData){
  50.  
  51.                         $nestedArrayItem['id'] = $itemData['id'];
  52.                         $nestedArrayItem['name'] = $itemData['name'];
  53.                      $nestedArrayItem['parent'] = $itemData['parent'];
  54.                         $nestedArrayItem['children'] = array();
  55.  
  56.                         insertIntoNestedArray($nestedArray, $nestedArrayItem);
  57.                     }
  58.                     header('Content-Type: application/json');
  59.                     header('Content-Type: text/html; charset=utf-8');
  60.                          $json= json_encode($nestedArray);
  61.                     echo $json = substr($json, 1, -1);
  62.  
  63.                     }
  64.                     ?>
  65.  
  66. json output
  67.               {
  68.    "id":"4",
  69.    "name":"B",
  70.    "parent":"0",
  71.    "children":[
  72.       {
  73.          "id":"5",
  74.          "name":"B1",
  75.          "parent":"4",
  76.          "children":[
  77.  
  78.          ]
  79.       }
  80.    ]
  81. }
Add Comment
Please, Sign In to add comment