<?php
//fetch.php
include_once "koneksidb.php";
//$connect = mysqli_connect("localhost", "root", "", "perpustakaandb");
$query = " SELECT * FROM t_menu";
$result = mysqli_query($koneksidb, $query);
//$output = array();
while($row = mysqli_fetch_array($result))
{
 $sub_data["id"] = $row["id"];
 $sub_data["name"] = $row["nama"];
 $sub_data["text"] = $row["nama"];
 $sub_data["parent_id"] = $row["parent_id"];
 $sub_data["level"] = $row["level"];
 $sub_data["url"] = $row["url"];
 $sub_data["icon"] = $row["icon"];
 $data[] = $sub_data;
}
foreach($data as $key => &$value)
{
 $output[$value["id"]] = &$value;
}
foreach($data as $key => &$value)
{
 if($value["parent_id"] && isset($output[$value["parent_id"]]))
 {
  $output[$value["parent_id"]]["nodes"][] = &$value;
 }
}
foreach($data as $key => &$value)
{
 if($value["parent_id"] && isset($output[$value["parent_id"]]))
 {
  unset($data[$key]);
 }
}
mysqli_free_result($result);
echo json_encode($data);
/*echo '<pre>';
print_r($data);
echo '</pre>';*/

?>