I have been doing searches for hours upon hours and have tried different snippets of codes and techniques and I simply cannot get any of them to work. I have done research, I am simply a very inexperienced developer trying to learn as I go.
My ultimate goal: Have a live-updating graph of test results data pulled from a MySQL database and rendered using Chart.JS library.
What I have done so far:
- I have an HTML file with embedded JavaScript and calls to AJAX function to auto-refresh page every 5 minutes (this has been tested and works).
- I have a PHP file which opens a PDO connection to a local MySQL database, runs 3 selection queries, and stores the results into 3 PHP arrays. (I have iterated through the arrays and echoed the results and the data is good).
- I have then stored the data inside these PHP arrays into a JSON object using json_encode()
e.g.
$totalTestsPassedArray = array(); $sql = "SELECT totalTestsPassed FROM execution ORDER BY lastDate"; $query = $conn->prepare($sql); $query->execute(array(':totalTestsPassed' => $totalTestsPassed)); while($row = $query->fetch()){ array_push($totalTestsPassedArray, $row['totalTestsPassed']); } $json_totalTestsPassedArray = json_encode($totalTestsPassedArray); //pack data into JSON object echo $json_totalTestsPassedArray; // for accessibility by AJAX foreach ($totalTestsPassedArray as $item){ //print out contents of array echo $item . '<br>'; }
I read that one method to consume this JSON data is to use jQuery's $.getJSON method, and in order to use it, I must 'echo' out the results of my json_encoded object. I realize I am printing the results twice. The echo $json_totalTestsPassedArray is for the aforementioned purpose.
- I have included the path of this PHP file in my HTML file from #1 using
<?php require '../file_name.php';?>
- I have tried several ways of unpacking this JSON encoded object packed with my database data: using AJAX requests, making a new array in the HTML and then flooding it with the JSON object using $.getJSON, and some other things people have suggested to do on various documents online, all met with failure.
Could someone please tell me the proper way to do what I am trying to do? I have a PHP file with an array of data, and I would like to pass that array of data into a dataset for use by Chart.JS to populate a graph and then draw it to the canvas and display my results.
var data = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
{
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data : [DATA FROM MY PHP ARRAY IN ANOTHER FILE WOULD GO HERE...]
}
Again, please forgive my ignorance. I have tried my best to figure out the solution on my own and would really appreciate some guidance. It's my first time using these tools. I thought it would be as simple as:
- Query DB using PHP script
- Store results into array
- Pack into JSON object (the goal being to transfer data from PHP to JS file)
- Unpack JSON object in JavaScript
- Load unpacked data into my JavaScript array
ajax
and an$.getJSON
expect to receive only ONE json-object. 2. $.getJSON does not do much more than the ajax, but receives json only (were as ajax can consume basicly everything like xml, html,..) – Jeff Jul 29 '15 at 0:32foreach
and post any errors you get (in your console (F12))! – Jeff Jul 29 '15 at 0:34