Advertisement
apl-mhd

Recursive Database Tree

Dec 24th, 2018
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.34 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: apelmahmud
  5.  * Date: 02/04/2017
  6.  * Time: 3:24 PM
  7.  */
  8.  
  9.  
  10.  
  11.     $serverName = 'localhost';
  12.     $userName = 'root';
  13.     $pass = '';
  14.     $dbname = 'HR';
  15.  
  16.     $name ="orko";
  17.  
  18.     $con = new  mysqli($serverName,$userName,$pass,$dbname);
  19.  
  20.     if($con->connect_error){
  21.  
  22.         die("connect fail");
  23.     }
  24.  
  25.    // $sql = 'select * from employees where employee_id = 3000';
  26.  
  27.    
  28.  
  29.  
  30. function tree($parent){
  31.  
  32.     global $con;
  33.     $sql = "select * from employees where manager_id =$parent";  
  34.     $result =  $con->query($sql);
  35.  
  36.     if ($result->num_rows > 0) {
  37.  
  38.    
  39.  
  40.     echo "<ul>";
  41.  
  42.     while ($row = $result->fetch_assoc()) {
  43.        
  44.         echo "</li>";
  45.             echo $row['EMPLOYEE_ID']." ".$row['FIRST_NAME']."<br>";
  46.         echo "</li>";
  47.  
  48.             tree($row['EMPLOYEE_ID']);
  49.    
  50.     }
  51.  
  52.     echo "</ul>";
  53.  
  54. }
  55.  
  56. }
  57.  
  58.  
  59.     //tree(3);
  60.     tree(1);
  61.  
  62.  
  63.     //before if conditon
  64.  
  65.     // <ul>
  66.     // </li>2 Diana<br></li>
  67.     // <ul>
  68.     // </ul>
  69.     //  </li>10 Nancy<br></li>
  70.     // <ul>
  71.     //  </li>11 Daniel<br>
  72.     // </li>
  73.     // <ul></ul>
  74.     // </ul></ul>
  75.  
  76.  
  77.     //after add if condition
  78.     // <ul>
  79.     //     </li>2 Diana<br></li>
  80.     //     </li>10 Nancy<br></li>
  81.     //     <ul>
  82.     //         </li>11 Daniel<br></li>
  83.     //     </ul>
  84.     // </ul>
  85.    
  86.    
  87.        
  88.        
  89.    
  90.  
  91.  
  92. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement