Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('Content-Type: application/json');
- $servername = "localhost";
- $username = "root";
- $password = "";
- $db = "kuliah";
- // Create connection
- $conn = mysqli_connect($servername, $username, $password, $db);
- // Check connection
- if (!$conn) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "SELECT DISTINCT(semester) FROM MATAKULIAH";
- $result = mysqli_query($conn,$sql);
- //var_dump($result);
- $semester = [];
- if (mysqli_num_rows($result) > 0) {
- // output data of each row
- while($row = mysqli_fetch_assoc($result)) {
- //echo $row["semester"]."<br>";
- array_push($semester, $row['semester']);
- }
- }
- mysqli_free_result($result);
- $jdw = [];
- foreach($semester as $val){
- $sql = "SELECT * FROM MATAKULIAH where semester='".$val."'";
- $result2 = mysqli_query($conn,$sql);
- if (mysqli_num_rows($result2) > 0) {
- $mhs =[];
- while($row = mysqli_fetch_assoc($result2)) {
- //echo $row["semester"]."<br>";
- array_push($mhs, array('nama'=>$row['nama'], 'sks'=> $row['sks'], 'dosen'=>$row['dosen'], 'ruangan'=>$row['ruangan'], 'jam'=>$row['jam']));
- }
- }
- array_push($jdw, array('title'=> $val, 'childreen'=> $mhs));
- mysqli_free_result($result2);
- }
- $final_result=json_encode($jdw);
- echo($final_result);
- mysqli_close($conn);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement