I have a php file names test.php which gives me a JSON output which is a valid output as I have validated in http://jsonlint.com/ .I want to use that JSON directly in my .html code which used D3.
test.php
<?php
$user = "root";
$password = "";
$database = "scheduler";
$con = mysqli_connect("localhost", $user, $password, $database) or die ("Unable to connect");
$query = "SELECT semone.userid AS id, semone.semester AS semester, semone.cname AS name, courses.credit AS value, courses.progskill AS skill
FROM semone
INNER JOIN courses
ON semone.cname = courses.coname" ;
$result = mysqli_query($con,$query)or die ("Unable to connect");
$lastId = null;
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$row['xyz'] = array(
'name'=> $row['name'],
'value'=> $row['value']
);
$info[$row['semester']]['semester'] = $row['semester'];
$info[$row['semester']]['children'][]= $row['xyz'];
$lastId = $row['id'];
}
// do not call json_encode on each iteration of the loop
$data = json_encode(array('id' => $row['id'], 'children' => array_values($info)));
// echo $data;
?>
The html code where I am trying to use the JSON created from test.php in D3.
tree.html
var canvas = d3.select("body").append("svg")
.attr("width",width)
.attr("height",height + padding);
var data;
d3.json("test3.php", function (error,json) {
if(error) return console.warn(error);
data = json;
console.log(data);
var treemap = d3.layout.treemap()
.size([550,550])
.nodes(data);
var cells = canvas.selectAll(".cell")
.data(treemap)
.enter()
.append("g")
.attr("class","cell")
It is not showing anything as if it is not able to access that JSON file. However When I am using the same JSON through some JSON file saved in the folder, it is working fine. So there is some problem in connection of this html and php file.