2016-03-15 135 views
0

谷歌API圖表沒有采取我使用PHP回聲的價值任何人都可以啓發我,我做錯了什麼?Google Chart API不適用於PHP?

這是用戶挑選他們想要查看作業圖表:

<form class="form-horizontal form-select-chart" method="post" action="analytics.php"> 
       <select name="chartname"> 
       <?php 
        $sel_user = "select * from tbl_jobs"; 
        $run_user = mysqli_query($con, $sel_user); 
        while($row = mysqli_fetch_assoc($run_user)) 
         { 
          echo "<option value=\"" . $row['Job_ID'] . "\">" . $row['Job_Name'] . "</option>"; 
         } 
       ?> 
       </select> 
       <br> 
       <input type="submit" name="make_chart" value="Create the Chart" class="btn btn-primary" /> 
       </form> 

這裏就是數據被處理:

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 
<script type="text/javascript"> 
<?php 
    if(isset($_POST['make_chart'])){ 
     $jid = mysqli_real_escape_string($con,$_POST['chartname']); 
     $sel_user = "select * from tbl_jobs WHERE `ID` = '$jid'"; 
        $run_user = mysqli_query($con, $sel_user); 
        while($row = mysqli_fetch_assoc($run_user)) 
         { 
         $jname = $row['Job_Name']; 
         $overall_vacant = $row['Vacant']; 
         $overall_filled = $row['Filled']; 
         } 
         settype($overall_vacant, "integer"); 
         settype($overall_filled, "integer"); 
    } 
?> 
    // Load the Visualization API and the corechart package. 
    google.charts.load('current', {'packages':['corechart']}); 

    // Set a callback to run when the Google Visualization API is loaded. 
    google.charts.setOnLoadCallback(drawChart); 

    // Callback that creates and populates a data table, 
    // instantiates the pie chart, passes in the data and 
    // draws it. 
    function drawChart() { 
    // Create the data table. 
    var data = new google.visualization.DataTable(); 
    data.addColumn('string', 'Topping'); 
    data.addColumn('number', 'Slices'); 
    data.addRows([ 
     ['Vacant Positions', <?php echo $overall_vacant; ?>], 
     ['Filled Positions', <?php echo $overall_filled; ?>], 
    ]); 

    // Set chart options 
    var options = {'title':'<?php echo $jname; ?>', 
        'width':400, 
        'height':300}; 

    // Instantiate and draw our chart, passing in some options. 
    var chart = new google.visualization.PieChart(document.getElementById('chart_div')); 
    chart.draw(data, options); 
    } 
</script> 

這裏是將要顯示的數據:

<?php 
     if(isset($_POST['make_chart'])){ 
     echo"<div align=\"center\" id=\"chart_div\"></div>"; 
     } 

     unset($_POST['make_chart'])); 
    ?> 
+1

您可以在表單提交後顯示腳本的樣子嗎?我猜'$ jname'不是你認爲的那樣。或者,'options.title'不應該引用它。 – MECU

+0

@MECU即使在選擇表單中有其他選項,它也只是空白並顯示標題「整體」 –

回答

0

經過進一步調查,我發現我的MySQL查詢出現了問題!我應該把「Job_ID」而不是「ID」這兩個單獨的東西!

我的表是這樣的:

ID | Job_ID | Job_Name | Vacant | Filled 
1  0  Overall 0  0 

這反過來又導致一個空白圖表!我真笨!感謝MECU的領導!