You must be logged in to post your query.
Home › Forums › Chart Support › PHP foreach loop won't render chart
Can someone help me with why my code won’t render:
<?php
$dataPoints = array(); //Best practice is to create a separate file for handling connection to database try{
$stmt = $conn->prepare(“SELECT created_on, sr_health FROM health_score_daily WHERE acct_id = $acct_id”); $stmt->execute(); $result = $stmt->fetchAll(\PDO::FETCH_OBJ);
foreach($result as $row){ array_push($dataPoints, array(“x”=> $row[‘created_on’], “y”=> $row[‘sr_health’])); } $conn = null; } catch(\PDOException $ex){ print($ex->getMessage()); }
?>
<script> window.onload = function () {
var chart = new CanvasJS.Chart(“chartContainer”, { animationEnabled: true, exportEnabled: true, theme: “light1”, // “light1”, “light2”, “dark1”, “dark2” title:{ text: “SuccessRocket (SR) Activity Score” }, data: [{ type: “column”, //change type to bar, line, area, pie, etc dataPoints: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?> }] }); chart.render();
} </script>
<div id=”chartContainer” style=”height: 250px; width: 100%;”></div> <script src=”/csm/canvasjs/canvasjs.min.js”></script>
@don-leesuccessrocket-io,
Can you kindly share sample database over Google-Drive or Onedrive, so that we can try running your code locally, understand the issue and help you out resolving it?
— Vishwas R Team CanvasJS
Simple table here:
https://docs.google.com/document/d/1TzNIurYQB2Py9tQjjK5TwbZCT9w-J54bg47H-H5X6d4/edit?usp=sharing
In the code that you have shared, connection to database is not established and the variable $acct_id is not declared which you are passing in the query. Fixing these two issues should work fine in your case. Please download the working code here.
$acct_id
You must be logged in to reply to this topic. Login/Register