Mena
Mena

Reputation: 2029

How can PHP variables be used in Google Visualization API to define rows of data

Working with Google visualization API for the first time and am trying to define row data using PHP variables in the function createChart(). The code am working with returns the error below

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in

This is the code am working with

function createChart() {

    //create data table object
    var dataTable = new google.visualization.DataTable();

    //define columns
    dataTable.addColumn('string','Name');
    dataTable.addColumn('number', 'Total');

    <?php
        $year=1;
        $subjectID=1;

        $result = mysqli_query($connection, "select * from fullresult where year='{$year}' and subject_id='{$subjectID}' order by firstname");
        while ($students = mysqli_fetch_assoc($result)) {
    ?>

    //define rows of data
    //this is the part of the code returning the error
    <?php echo "dataTable.addRow(['$students[\"firstname\"]',$students[\"total\"]])"; } ?>

    //instantiate our chart object
    var chart = new google.visualization.ColumnChart (document.getElementById('chart'));

    //define options for visualization
    var options = {width: 400, height: 240, is3D: true, title: 'Student Performance Summary'};

    //draw our chart
    chart.draw(dataTable, options);

}

Unfortunately I haven't been able to find any question similar to this. So how do i effectively use variables to define the data row without having errors?

Comments and corrections are welcome and would be highly appreciated as i look forward to learning more of this.

Upvotes: 1

Views: 84

Answers (1)

WhiteHat
WhiteHat

Reputation: 61230

think it's bombing on the escape characters

try something like this instead...

<?php echo "dataTable.addRow(['".$students['firstname']."',".$students['total']."]);"; } ?>

be sure to include the ; for the JavaScript statement

Upvotes: 1

Related Questions