2017-09-05 47 views
0

Greetings stackoverflow! 我目前把一些手放在圖表上,特別是谷歌圖表和使用php和phpmyadmin,我試圖從普通查詢和結果集中的數據庫(課程名稱+一個餅圖的數字)拉結果,但是我只在循環時纔得到一行,我會在下面發佈我的代碼,以便在可能的情況下獲得一些提示。 (固定它,首先檢查ANSWER)使用php和mysql的GoogleCharts

<?php 
    include('../session.php'); 
include("../config.php"); 
    ?> 

<?php 




$sqlMostTakenCourse="SELECT course_title, count(*) 
FROM wp_wpcw_user_courses, wp_wpcw_courses 
WHERE wp_wpcw_user_courses.course_id = wp_wpcw_courses.course_id 
AND wp_wpcw_user_courses.course_progress >0 
AND wp_wpcw_user_courses.user_id 
IN (

SELECT wp_users.ID 
FROM wp_users, wp_usermeta, jdashboard 
WHERE wp_usermeta.meta_key = 'CouponCode' 
AND jdashboard.username = 'jad' 
AND wp_usermeta.meta_value = jdashboard.coupon 
AND wp_usermeta.user_id = wp_users.ID 
) 
GROUP BY course_title 
ORDER BY count(*) DESC "; 



mysqli_set_charset($db,"utf8"); 

$resultMostTakenCourse = mysqli_query($db, $sqlMostTakenCourse); 



    if(mysqli_num_rows($resultMostTakenCourse) > 0){ 

     while($row = mysqli_fetch_array($resultMostTakenCourse)){ 



    ?> 


<html> 
    <head> 
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 
    <script type="text/javascript"> 
     google.charts.load('current', {'packages':['corechart']}); 
     google.charts.setOnLoadCallback(drawChart); 

     function drawChart() { 

     var data = google.visualization.arrayToDataTable([ 
      ['Task', 'Hours per Day' ], 

      [ ' <?php echo $row[0]; ?>', <?php echo $row[1];  ?>  ], 

      ['Sleep', 7] 
     ]); 
<?php }} ?> 
     var options = { 
      title: 'My Daily Activities' 

     }; 

     var chart = new google.visualization.PieChart(document.getElementById('piechart')); 

     chart.draw(data, options); 
     } 
    </script> 
    </head> 
    <body> 
    <div id="piechart" style="width: 900px; height: 500px;"></div> 
    </body> 
</html> 

[ ' <?php echo $row[0]; ?>', <?php echo $row[1]; ?> ],從數據庫中提取唯一1行,我將回顧我的代碼,看看有什麼遺漏,並希望得到一些幫助和提示。 編輯:我意識到循環沒有根據行創建['名稱',數字],將嘗試解決該問題

+0

你應該意識到,這是沒有意義的輸出完整的HTML文檔中的循環...'html','head','都必須出現body'內只有一次,而不是多個倍。 – CBroe

+0

謝謝你,我已經把他們從循環中拿出來了,但是你有任何其他的想法或小費來解決這個問題嗎? – Lalati

+0

那麼你也_overwriting_你的JS函數'drawChart'在每個循環迭代... – CBroe

回答

0

我修復了使用data.addRow()函數和初始化變量之前循環,修改代碼如下:

<html> 
    <head> 
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 
    <script type="text/javascript"> 
     google.charts.load('current', {'packages':['corechart']}); 
     google.charts.setOnLoadCallback(drawChart); 

     function drawChart() { 
var data = new google.visualization.DataTable(); 
data.addColumn('string', 'Course'); 
data.addColumn('number', 'Count'); 

<?php 



     while($row = mysqli_fetch_array($resultMostTakenCourse)){ 



    ?> 


data.addRow(['<?php echo $row[0]; ?>', <?php echo $row[1]; ?>]); 

     <?php } ?> 

     var options = { 
      title: 'Courses/Registered times' 

     }; 

     var chart = new google.visualization.PieChart(document.getElementById('piechart')); 

     chart.draw(data, options); 
     }