Home Forums Chart Support PHP foreach loop won't render chart

PHP foreach loop won't render chart

Viewing 4 posts - 1 through 4 (of 4 total)
  • #22848

    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>

    #22871

    @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

    #22885
    #22890

    @don-leesuccessrocket-io,

    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.


    Vishwas R
    Team CanvasJS

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.