2015-06-23 42 views
1

我想在餅圖中顯示員工數據。我能夠使用jQuery和PHP製作餅圖。但需求是在一個頁面中有12個按鈕。名稱1月至12月按月顯示數據

當用戶單擊feb然後feb的餅圖顯示給用戶。當點擊十二月的餅圖時。顯示給用戶。

我的數據庫中有一個date列,其中日期存儲像2015年6月23日

我不能獲得SQL查詢的邏輯,這裏是我的代碼,但它需要改進。

<html> 
    <head> 
    <!--Load the AJAX API--> 
    <script type="text/javascript" src="https://www.google.com/jsapi"></script> 
    <script type="text/javascript"> 

     // Load the Visualization API and the piechart package. 
     google.load('visualization', '1.0', {'packages':['corechart']}); 

     // Set a callback to run when the Google Visualization API is loaded. 
     google.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 
     ([ 
      <?php 

      $servername = "localhost"; 
      $username = "root"; 
      $dbname  = "dat_database"; 
      $password = ""; 
      $conn  = new mysqli($servername, $username, $password, $dbname); 
      $EMP_ID = 'MMUM254'; 
      $sql  = "SELECT typeof_task,hourspend from emp_dat_info where EMP_ID='" . $EMP_ID. "' ORDER BY date DESC LIMIT 10 " ; 
      $result  = $conn->query($sql); 
      if ($result->num_rows > 0) { 
          while ($row = $result->fetch_assoc()) { 
           $a[] = "['" . $row["typeof_task"] . "', " . $row["hourspend"] . "]"; 
       } 
       echo implode(', ', $a); 

} 

?> 

     ]); 

     // Set chart options 
     var options = {'title':'How Much Hours You Spend', 
         'width':900, 
         'height':500}; 

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

    <body> 
    <!--Div that will hold the pie chart--> 
    <div id="chart_div"></div> 
    </body> 
</html> 
<?php echo date("Y"); ?> 

`

回答

1

你可以嘗試month子句中的代碼來獲得單月數據

$sql = "SELECT typeof_task,hourspend 
     FROM emp_dat_info 
     WHERE EMP_ID='" . $EMP_ID. "' 
     ORDER BY MONTH(date) DESC LIMIT 10 " ; 
     $result  = $conn->query($sql); 
1

如果你想獲得一個月的全名,那麼你可以嘗試MONTHNAME子句中查詢。在MONTHNAME子句中傳遞日期。

$sql = "SELECT typeof_task,hourspend 
     FROM emp_dat_info 
     WHERE EMP_ID='" . $EMP_ID. "' 
     ORDER BY MONTHNAME(date) DESC LIMIT 10 " ; 
     $result  = $conn->query($sql); 

更多的參考,您可以訪問此鏈接:帶有日期https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_month

1

最好每月

$sql = "SELECT typeof_task,hourspend 
      FROM emp_dat_info 
      WHERE EMP_ID='" . $EMP_ID. "' 
      ORDER BY MONTH(date) DESC LIMIT 10 " ; 
      $result= $conn->query($sql);