2016-05-31 79 views
1

首次使用Google可視化API,並試圖在函數createChart()中使用PHP變量定義行數據。該代碼正在與工作回報低於在Google Visualization API中如何使用PHP變量來定義數據行

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

這是代碼我工作的錯誤與

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); 

} 

可惜我一直沒能找到類似這樣的任何問題。那麼我如何有效地使用變量來定義數據行而不會出錯呢?

評論和更正是受歡迎的,將受到高度讚賞,因爲我期待更多地瞭解這一點。

回答

1

認爲它轟炸的轉義字符

嘗試這樣的事情,而不是...

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

一定要包括;的JavaScript語句

+0

完美工作,謝謝! – Mena

相關問題