PHP Mysql Highcharts多查询问题

时间:2014-03-27 18:24:29

标签: php mysql highcharts

我正在尝试构建一个网页,其中将显示图表,并从下拉菜单中加载数据。我有2个数据集,但我认为无法执行正确的MySql请求。 2个数据集存储在同一个数据库中,但存储在不同的表中。我不能将它们放在同一个表中,因为它们的长度和频率不同。(一个是月度数据,另一个是每周一次)

这是代码。非常感谢任何帮助!

<?php

 mysql_connect('xxxxxxxxxxxx.com', 'xxxxx', 'xxxxxx') or die(mysql_error());
 mysql_select_db("database123") or die(mysql_error());


 $result1 = mysql_query("SELECT * FROM table1")
 or die(mysql_error());  

 $data1  = array();


 while ($row1 = mysql_fetch_array( $result1)) {

  extract($row1);

    $datetime1 = date('Y, n, j', strtotime($table1_date )); 

    $datetime2 = 'Date.UTC('.$datetime1.')'; 


     $data[] = "[$datetime2, $table1_data]";


    }

  ?>

 <?php

 mysql_connect('xxxxxxxxxxxx.com', 'xxxxx', 'xxxxxx') or die(mysql_error());
 mysql_select_db("database123") or die(mysql_error());


 $result2 = mysql_query("SELECT * FROM table2")
 or die(mysql_error());  

 $data2  = array();


 while ($row2 = mysql_fetch_array( $result2)) {

  extract($row2);

    $datetime3 = date('Y, n, j', strtotime($table2_date )); 

    $datetime4 = 'Date.UTC('.$datetime3.')'; 


    $data2[] = "[$datetime4, $table2_data]";


    }

   ?>




   <!DOCTYPE html>
   <html lang="en-US">
  <head>

<meta charset="utf-8" />


     <script type="text/javascript"  src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>


    <script type="text/javascript">

      $(function () {

      var options = {
     chart: {
     renderTo: 'container',
     defaultSeriesType: 'column'
    }, 
     series: [{name: 'A', data: [<?php echo join($data, ',') ?>]}]
    };
    var chart = new Highcharts.Chart(options);

            });


     $("#list").on('change', function(){
  //alert('f')
  var selVal = $("#list").val();
  if(selVal == "A" || selVal == '')
     {
    options.series = [{name: 'A', data: [<?php echo join($data, ',') ?>]}]
     }
     else
     {
     options.series = [{name: 'D', data: [<?php echo join($data2, ',') ?>]}]
     }  
     var chart = new Highcharts.Chart(options);    
     });

     </script>


     </head>
     <body id="index" class="home">



     <SELECT id="list">
     <option VALUE="A">Data Set A</option>
     <option VALUE="B">Data Set B</option>

     </SELECT>
     <button id="change">Refresh Table</button>

     <div id="container" style="min-width: 350px; height: 400px; margin: 0 auto"></div>



     <script src="http://code.highcharts.com/highcharts.js"></script>
    <script src="http://code.highcharts.com/modules/exporting.js"></script>



     </body>
     </html>

1 个答案:

答案 0 :(得分:0)

我建议准备php脚本,它将获取参数表单url(如$ _GET [&#39; table&#39;]),并调用数据库查询。之后,通过json_encode()返回正确的数组。在javascript中你可以使用$ .getJSON()并从php加载数据作为json,然后使用highcharts。