<?php
//fetc.php
$connect = mysqli_connect("localhost", "root", "", "shopp");
$query = " SELECT * FROM maincategory ";
$result = mysqli_query($connect, $query);
//$output = array();
while($row = mysqli_fetch_array($result))
{
$sub_data["id"] = $row["id"];
$sub_data["category"] = $row["category"];
$sub_data["text"] = $row["category"];
$sub_data["p_id"] = $row["p_id"];
$data[] = $sub_data;
}
foreach($data as $key => &$value)
{
$output[$value["id"]] = &$value;
}
foreach($data as $key => &$value)
{
if($value["p_id"] && isset($output[$value["p_id"]]))
{
$output[$value["p_id"]]["nodes"][] = &$value;
}
}
foreach($data as $key => &$value)
{
if($value["p_id"] && isset($output[$value["p_id"]]))
{
unset($data[$key]);
}
}
echo json_encode($data);
/*echo '
';
print_r($data);
echo '
';*/
?>
$(document).ready(function(){
$.ajax({
url: "fetc.php",
method:"POST",
dataType: "json",
success: function(data)
{
$('#treeview').treeview({data: data});
}
});
});