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:
$(".<?php echo $id ?>").click(function(e) {
....
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.
$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>';
}
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.
<?php require '../file_name.php';?>
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:
答案 0 :(得分:0)
仅在php打印json
并将标题content type
设置为application/json
,然后您的XHR
响应将包含您的数据的javascript对象。不需要任何json解析。