dongxuxian1123 2014-10-13 10:51
浏览 52
已采纳

highchart mysql json with dropdown select value for where condition

i'm trying to use highcharts using json data from mysql table. i want to set a drop down list with values that are going to be used as WHERE conditions for the mysql_query to fetch selected data and populate them in highchart. the codes are working properly with static conditions (no drowpdown variables).

main.html

<form method="get" action="" >
Chose :
<select name="liste" id="liste">
<option value="A" <? if($selected == 'A'){ echo 'selected="Choice A"';}?>Choice A</option>
<option value="B" <? if($selected == 'B'){ echo 'selected="Choice B"';}?>Choice B</option>
<option value="C" <? if($selected == 'C'){ echo 'selected="Choice C"';} ?>Choice C</option>
<option value="D" <? if($selected == 'D'){ echo 'selected="Choice D"';} ?>Choice D</option>
</select>
<input type="submit" value="Go" />
</form>

data.php

`<?php

$A=$_GET['liste'];

$con = mysql_connect("localhost","myuser","mypwd");

if (!$con) {
die('Could not connect: ' . mysql_error());
 }

mysql_select_db("mydatabase", $con);

$test = mysql_query("SELECT name FROM table1 WHERE table2.age LIKE '{$A}'");

$rowss = array();
while($rr = mysql_fetch_array($test)) { 
$ro[0] = $rr[0];
$ro[1] = $rr[1];
array_push($rowss,$ro); 
}

 print json_encode($rowss, JSON_NUMERIC_CHECK);

 mysql_close($con);
?>

on the main.html file i have the highchart script that call the data.php file to get the json data.

<script>
     $(document).ready(function() {
  //rest of the code
 $.getJSON("data.php", function(json) {
            options.series[0].data = json;
            chart = new Highcharts.Chart(options);
        });
</script>

testing the data.php give the correct output according to the selected value. so i'm assuming that it is working fine. my problem is when loading the main.html it draws an empty graph and even when changing the value keep drawing empty data graph.

NB: i know mysql_* is no longer best practice and planing to switch to mysqli_* later.

EDIT: seems like my options.series[0] options obj is not created and the console shows empty data array. when using <form method="get" action="data.php" > i got the expect array but cannot get the graph.

  • 写回答

2条回答 默认 最新

  • dongxiang3648 2014-10-14 07:22
    关注

    I got works as i wanted according to the following example i had to make ajax call to set the variable onchange of the drop menu and the code become like this (remove the form tag and submit button) main.html:

    <select name="list" id="list">
    <option value="A">Choice A</option>
    <option value="B">Choice B</option>
    <option value="C">Choice C</option>
    <option value="D">Choice D</option>
    </select>
    
    <script>
     $(function () {
    
      //on page load  
     getAjaxData("A");
    
     //on changing select option
     $('#list').change(function(){
     var val = $('#list').val();
     getAjaxData(val);
     });
    
     function getAjaxData(id){
     //use getJSON to get the dynamic data via AJAX call
     $.getJSON('data.php', {id: id}, function(chartData) {
     .....rest of the code
    

    and the data.php:

     <?php
    
     $con = mysql_connect("localhost","myuser","mypwd");
    
     if (!$con) {
     die('Could not connect: ' . mysql_error());
     }
    
     mysql_select_db("mydatabase", $con);
    
     $id = $_GET['id'];
    
     $test = mysql_query("SELECT name FROM table1 WHERE table2.age LIKE '{$id}'");
    
     $rowss = array();
      while($rr = mysql_fetch_array($test)) { 
      $ro[0] = $rr[0];
      $ro[1] = $rr[1];
     array_push($rowss,$ro); 
     }
    
     print json_encode($rowss, JSON_NUMERIC_CHECK);
    
      mysql_close($con);
     ?>
    

    enjoy it!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行