2

I created a PHP file to query JSON output. JSON output from the PHP file for a particular filter "testPHP.php?number=123" is

[{"source":"AB","target":"AC","type":"true"},{"source":"UB","target":"EP","type":"true"},{"source":"US","target":"UR","type":"lie"},{"source":"BS","target":"QW","type":"lie"},{"source":"UW","target":"EA","type":"lie"}]

I have tried this in html file to read the JSON output to links variable

var links; // a global
d3.json("testPHP.php?number=123", function(error, json) {
  if (error) return console.warn(error);
  links = json;
});

But, it seems like data is not stored in links. How do I store the data in var links?

Basically, I want to replace var links in this https://gist.github.com/mbostock/1153292 to that from JSON output from PHP.

EDIT: Or http://localhost:8080 is causing the problem?

2
  • Your JSON is invalid. How are you producing this JSON? Your last object in the array is missing a quote. Commented May 12, 2013 at 21:33
  • Typo error. Changed now. Commented May 12, 2013 at 21:50

2 Answers 2

2

The d3.json call is asynchronous; the code included after your callback is executed before the "links" variable has been populated.

var links; // a global
d3.json("testPHP.php?number=123", function(error, json) {
  if (error) return console.warn(error);
  links = json;

  console.log(links); // your links are populated here

  // include the rest of your app here
});

// links is still undefined here because the success callback hasn't been run yet
Sign up to request clarification or add additional context in comments.

Comments

-1

you would need to write links = [{"source":"AB","target":"AC","type":"true"},{"source":"UB","target":"EP","type":"true"},{"source":"US","target":"UR","type":"lie"},{"source":"BS","target":"QW","type":"lie"},{"source":"UW,"target":"EA","type":"lie"}]

At that point links will hold that information. You may however want to consider an array.

1 Comment

i need to use the json output from php file!

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.