0

I have a D3 graph which reads a JSON file. Where I am struggling is, what is the mechanism to get the dynamically generated PHP JSON data into the D3 graph.

I think the obvious solution from someone like myself with limited knowledge would be to have PHP write a JSON file to a directory on load, and then have the D3 graph look for it.

However, I feel like there is a better way to do it without writing a json file and reading a json file.

Could anyone please suggest the industry standard method for something like this, so I can continue my reading.

4
  • 1
    If you're talking about dynamic content, you'd typically have the client side make an AJAX request to the server (via JavaScript), and then PHP on the server would generate the JSON file on the fly. Commented Sep 19, 2018 at 16:06
  • 1
    Instead of pointing D3 to a static file, point it to your PHP file and have your PHP output JSON. Commented Sep 19, 2018 at 16:07
  • 1
    If you're dynamically creating the HTML page using PHP, you can write the appropriate JSON as part of the page code. Commented Sep 19, 2018 at 16:07
  • 1
    Your question is rather broad at present; can you refine it to talk about the specific situation that you are dealing with, ideally with a little code sample? As-is, potential responders don't know whether you're dealing with a PHP-based API, something like WordPress where page content is generated by PHP, etc. Thank you! Commented Sep 19, 2018 at 16:14

1 Answer 1

1

Just request the PHP script from D3 like it is a JSON file. Then in your PHP script...

$data = array('your', 'data', 'here');

header('Content-Type: application/json');

echo json_encode($data);

exit();
Sign up to request clarification or add additional context in comments.

Comments

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.