2016-08-07 50 views
0

我有兩個文件,index.phpshowday.php使用datepicker的新數據刷新PHP中的getJSON

index.php應該與canvasjs

的canvasjs本數據來源於showday.php的幫助下,它運行的MySQL查詢來提取數據

這裏顯示的時候就顯示了恰當的圖表年的文件顯示圖形,月和日變量在showday.php中有硬編碼值,因此我知道這個概念有效。

我的問題是: - 如何使用發送到showday.php當前日期加載index.php和 - 如何刷新$.getJSON由日期選擇器

<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="jqueryui/themes/sunny/jquery-ui.css"> 
    <link rel="stylesheet" href="jqueryui/style.css"> 
    <script src="jquery/jquery-3.1.0.js"></script> 
    <script src="jqueryui/jquery-ui.js"></script> 
    <script src="canvas/jquery.canvasjs.min.js"></script> 
    <script> 

    $(function() { 
     $("#datepicker").datepicker({ 
      changeMonth: true, 
      changeYear: true 
     }); 
    }); 


I guess that changedate function goes here with new date. 
But how to put that date in the call to: 
$.getJSON("showday.php", function (result) ??? 

Also, I would like that the page loads with current date, 
and then a user can refresh the graph with a chosen date 
with help of datepicker. How to do that? 

    $(document).ready(function() 
     { 

      $.getJSON("showday.php", function (result) 
       { 
        var chart = new CanvasJS.Chart("chartContainer", 
         { 
          zoomEnabled: true, 
          axisY:{ 
           title: "Power", 
           includeZero: false, 
           suffix : " kW", 
           }, 
          axisX: { 
           title: "Time", 
           }, 
          data: [ 
           { 
            type: "spline", 
            lineColor: "#FFAA00", 
            lineThickness: 2, 
            markerColor: "#007700", 
            dataPoints: result 
           } 
          ] 
         } 
        ); 
        chart.render(); 
       } 
      ); 
     } 
    ); 

    </script> 
</head> 

<body> 
<div id="chartContainer" style="width: 800px; height: 400px;"></div> 
<form action=""> 
    Date: <input type="text" id="datepicker" onchange="changedate(this.value)"> 
</form> 

</body> 
</html> 

showday.php

<?php 

    $con = mysqli_connect('localhost', 'db', 'password', 'table'); 

// Check connection 
if (mysqli_connect_errno($con)) 
{ 
    echo "Failed to connect to DataBase: " . mysqli_connect_error(); 
}else 
{ 
    $data_points = array(); 

**How the new date vlues coming from index.php can be plugged in here?** 

    $query = "SELECT Hour, Minute, PAC as kW FROM `logdata` WHERE (Year=?) AND (Month=?) AND (Day=?)"; 
    $result = mysqli_query($con, $query); 

    while($row = mysqli_fetch_array($result)) 
    { 
    $Time = $row['Hour'].":".$row['Minute']; 
     $point = array("label" => $Time , "y" => $row['kW']); 

     array_push($data_points, $point);   
    } 

    echo json_encode($data_points, JSON_NUMERIC_CHECK); 
} 
mysqli_close($con); 

?> 
選擇新的日期

回答

0

創建功能:

function updateChart() { 
    $.getJSON("showday.php", {date: $('#datepicker').datepicker('getDate')}, function (result) 
      { 
       var chart = new CanvasJS.Chart("chartContainer", 
        { 
         zoomEnabled: true, 
         axisY:{ 
          title: "Power", 
          includeZero: false, 
          suffix : " kW", 
          }, 
         axisX: { 
          title: "Time", 
          }, 
         data: [ 
          { 
           type: "spline", 
           lineColor: "#FFAA00", 
           lineThickness: 2, 
           markerColor: "#007700", 
           dataPoints: result 
          } 
         ] 
        } 
       ); 
       chart.render(); 
      } 
     ); 
} 

撥打updateChart()datepicker select$(document).ready()Pass data through $.getJSON通過傳遞一個對象作爲第二個參數。

使用php(在您的showday.php)使用$_GET['date']訪問date。有關如何從提供的字符串中提取年/日/月的信息,請參閱this post

+0

你好FrankerZ,我只是試過你的建議。有用!非常非常感謝你。 – araut