Advertisement
Guest User

Contoh Array Mahasiswa

a guest
Jan 27th, 2020
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. <?php
  2. header('Content-Type: application/json');
  3. $servername = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $db = "kuliah";
  7.  
  8. // Create connection
  9. $conn = mysqli_connect($servername, $username, $password, $db);
  10.  
  11. // Check connection
  12. if (!$conn) {
  13. die("Connection failed: " . $conn->connect_error);
  14. }
  15.  
  16.  
  17. $sql = "SELECT DISTINCT(semester) FROM MATAKULIAH";
  18. $result = mysqli_query($conn,$sql);
  19. //var_dump($result);
  20.  
  21. $semester = [];
  22. if (mysqli_num_rows($result) > 0) {
  23. // output data of each row
  24. while($row = mysqli_fetch_assoc($result)) {
  25. //echo $row["semester"]."<br>";
  26. array_push($semester, $row['semester']);
  27. }
  28. }
  29.  
  30. mysqli_free_result($result);
  31. $jdw = [];
  32.  
  33. foreach($semester as $val){
  34.  
  35. $sql = "SELECT * FROM MATAKULIAH where semester='".$val."'";
  36.  
  37. $result2 = mysqli_query($conn,$sql);
  38.  
  39. if (mysqli_num_rows($result2) > 0) {
  40. $mhs =[];
  41.  
  42. while($row = mysqli_fetch_assoc($result2)) {
  43. //echo $row["semester"]."<br>";
  44. array_push($mhs, array('nama'=>$row['nama'], 'sks'=> $row['sks'], 'dosen'=>$row['dosen'], 'ruangan'=>$row['ruangan'], 'jam'=>$row['jam']));
  45. }
  46. }
  47.  
  48. array_push($jdw, array('title'=> $val, 'childreen'=> $mhs));
  49.  
  50. mysqli_free_result($result2);
  51.  
  52. }
  53. $final_result=json_encode($jdw);
  54. echo($final_result);
  55.  
  56. mysqli_close($conn);
  57. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement