1

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.

1
  • Strange server code. You got $data output commented and $row['id'] exists only in while loop. You are trying to use it in json_encode outside of it. Commented Oct 21, 2015 at 6:21

1 Answer 1

1

Uncomment the echo $data . It should work fine.

Sign up to request clarification or add additional context in comments.

1 Comment

I am sorry for such a lame error. I just forgot to uncomment it and was looking for error all around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.