PHP to generate JSON for D3 - javascript

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.

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();

Related

How to transfer data from PHP to Smarty?

I have a project that uses Smarty files, but i don't understand how are they parsed from PHP to tpl. I understand that from tpl files to PHP are parsed with js that uses AJAX.
And please share some good AJAX guides, i mostly find people using this nonsense to parse PHP data to javascript
<script type="text/javascript">
var a = <?php echo json_encode($var); ?>;
</script>
To parse data to smarty template I needed to parse the variable in data array or in some modules parse object_data['data'] array

Create XML with php variables in javascript

I am wanting to create and save, to the server, an xml file. I understand I should use javascript but am not sure exactly how to code it.
Scenario:
I have a php page with variables, $firstname and $lastname for example.
So in the php:
$firstname = "John";
$lastname ="Doe";
I want to have a javascript that when fired will create and save an xml file.
I found the below post and it got me headed in the right direction.
How to save XML using PHP
Can anyone help me. I greatly appreciate some direction.
Additionally is it possible to have it outputjust like :
<?xml version="1.0" encoding="UTF-8"?>
<form1>
<fname>John</fname>
<lname>Doe</lname>
</form1>
Javascript is a client-side technology, PHP is server side. The question you linked is a good place to start, you'll want to use DomDocument and/or simplexml. If there is client side interaction, you'll still have to post the data to the server and store the file there.

json_encode works in php, but null in javascript

I'm just learning php, and I am trying to parse some files I have, and then send the information to javascript. This is my php:
$datesArray = readFile();
echo json_encode($datesArray);
This echoes:
["120102000000","120102000500","120102001000","120102001500","120102002000","120102002500","120102003000"]
Which is what I want. However, my javascript, which looks like this:
var dates = <?php echo json_encode($datesArray); ?>;
console.log(dates);
keeps giving dates back as null, and I am not sure why. I have tried encoding the array as utf-8, and I have tried using jQuery.parseJSON which also did not work.
Thanks--
Just checking, but is that JavaScript with PHP in a .js file? By default, Apache only runs PHP in files ending in .php so that would mean your PHP is getting parsed as plain text by the server.
If that is the case, the easiest solution is to run that part of the script in some <script> tags.
Alternatively you could make Apache try to serve .js files as PHP file via Mime-Types; but that is really a mess not worth implementing.

Phonegap IOS app, JSON parse error?

I am generating a json object from php with json_encode and consume it with JS. The JSON object is valid, i run it through JSON lint, but I am always getting a parseerror when it is consumed from the php file directly.
Now, if i generate a .json file and output the object there no error is raised.
Has anyone come across this problem? Any thoughts as to why this might be happening?
Thank you. Any help is greatly appreciated.
Try not passing the JSON object to the JS file but instead echo it out on the php file then tell js in an output function of some sort to pick it up1
eg
echo json_encode // In php file
function(output){
('some element').html(output); //output is what is returned from the php file (the json)
}

How Can I scrape the Google Keyword Tool?

I want to extract data from the Google keyword tool - https://adwords.google.com/select/KeywordToolExternal. Their site is in Javascript, the script I've been working on is in PHP. Anyway to do this?
Alternatively, if there is no good solution, I was thinking of downloading the csv file and extracting data from it. Unfortunately, I've never done something where I download a csv file then extracted data from it. Can anyone point me in the right direction?
Scraping the Keyword tool is not easy (and it breaks the terms and conditions).
If you download the CSV file you can read it with php (it is a plain text file). You will have to create the logic to use the data in the file.
this will get you started
$file_handle = fopen("myfile", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
echo $line;
}
fclose($file_handle);

Categories

Resources