简体   繁体   English

在同一页面上将F(x)转换为php到Highchart

[英]onchange F(x) to php to Highchart on same page

I am continuing a previous question that was asked onclick -> mysql query -> javascript; 我继续前面的问题,它是在onclick-> mysql query-> javascript上被询问的 same page 同一页

This is my onchange function for a drop down of names. 这是我的onchange函数,用于下拉名称。 it is called when each drop down is changed. 每次更改下拉菜单时都会调用它。 The idea is to send each runners name into the php page to run a mysql query then return 3 arrays to be entered into javascript. 想法是将每个跑步者的姓名发送到php页面以运行mysql查询,然后返回3个要输入到javascript中的数组。

function sendCharts() {
var awayTeam = document.getElementById('awayRunner').value;
var homeTeam = document.getElementById('homeRunner').value;        


if(window.XMLHttpRequest) {
    xmlhttp14 = new XMLHttpRequest();

}
else {
    xmlhttp14 = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp14.onreadystatechange = function() {
    if(xmlhttp14.readyState == 4 && xmlhttp14.status == 200) {

   var parts = xmlhttp14.responseText.split(','); //THIS IS WHAT IS RETURNED FROM THE MYSQL QUERY. WHEN I ALERT IT, IT OUTPUTS IN THE FORM 14,15,18,16,17,12,13 

          ... code that generates the chart 
        series: [   {
                        name: document.getElementById('awayRunner').value,
                        data: [parts,','], //THIS IS WHERE AN ARRAY MUST BE ENTERED. THIS OUPUTS ONLY ONE NUMBER 
                        type: 'column',
                        pointStart: 0
                        //pointInterval
                    },

                    {

                        name: document.getElementById('homeRunner').value,
                        data: parts, // TRIED THIS
                        type: 'column',
                        pointStart: 0
                        //pointInterval
                    },

                    {
                        name: 'League Avg',
                        data: [], //THIS IS WHERE 3rd ARRAY MUST BE ENTERED
                        type:'spline',
                        pointStart: 0
                        //pointInterval
                    },
                ]
    });

    }
}
xmlhttp14.open("GET", "getCharts.php?awayRunner="+awayRunner+"&homeRunner="+homeRunner, true);
xmlhttp14.send();

} }

my php code looks like this. 我的PHP代码看起来像这样。 As you'll see, there are 3 arrays that must be returned to be entered into different spots in the javascript to generate the code. 正如您将看到的,必须返回3个数组才能将它们输入到javascript中的不同位置以生成代码。

$away=$_GET['awayRunner'];
$home=$_GET['homeRunner'];

$db=mydb;



$homeRunner=array();
$awayRunner = array();
$totalOverall= array();

$getHome="select column from $db where tmName = '$home'";
$result2 = mysql_query($getHome);
while($row = mysql_fetch_array($result2)){
$homeRunner[]= $row['column'];
}

$getAway="select column from $db where tmName ='$away'";
$result22 = mysql_query($getAway);
while($row2 = mysql_fetch_array($result22)){
$awayRunner[]= $row2['column'];

}

$week = 0;
while($week<20){
$week++;
$teamCount = "select count(column) from $db where week = $week";
$resultTeam =  mysql_query($teamCount);
$rowTeam = mysql_fetch_array($resultTeam);
$t = $rowTeam['count(column)'];

$getLeague = "select sum(column) from $db where week = $week";
$resultLeague = mysql_query($getLeague);
while($row3 = mysql_fetch_array($resultLeague)){
    $totalOverall[]=$row3['sum(column)']/$t;
    }
}
echo join(',',$awayRunner);

currently, by doing it this way, the chart only outputs the second value in the array. 当前,通过这种方式,图表仅输出数组中的第二个值。 for instance, if var parts is equal to 23,25,26,24,23...only 25 is shown. 例如,如果var part等于23,25,26,24,23 ...仅显示25。

A previous question resulted with the following answer - 前一个问题的答案如下:

  1. Load the page. 加载页面。
  2. User chooses an option. 用户选择一个选项。
  3. An onChange listener fires off an AJAX request 一个onChange侦听器触发AJAX请求
  4. The server receives and processes the request 服务器接收并处理请求
  5. The server sends back a JSON array of options for the dependent select 服务器发回一个JSON数组的选项供相关选择
  6. The client side AJAX sender gets the response back 客户端AJAX发送者获得响应
  7. The client updates the select to have the values from the JSON array. 客户端更新选择以具有JSON数组中的值。

I'm lost on #'s 5 - 7. Can someone provide examples of code that gets this done? 我迷上了#的5-7。有人可以提供示例代码来完成此操作吗? Normally, I would just ask for direction, but I have been stuck on this problem for days. 通常,我只是问路,但是我已经在这个问题上停留了好几天。 I'm about ready to scrap the idea of having charts on my site. 我准备放弃在网站上放置图表的想法。 Thanks in advance 提前致谢

EDIT

this is the first change that I have made to send and receive just one request 这是我发送和接收一个请求的第一个更改

<script>
$(function(){
    $("#awayRunner").change(function(){ 
        $.ajax({
        type: "POST",    
        data: "data=" + $("#awayRunner").val(),
        dataType: "json",
        url: "/my.php",
        success: function(response){
            alert(response);  
        }
        });
    });
});

The data displayed in the alertbox is in the form 12,15,16,15. 警报框中显示的数据格式为12,15,16,15。 Now, when I enter in data: response, only the second number from each is being displayed in the chart. 现在,当我输入data:响应时,图表中仅显示每个数字的第二个数字。 Any ideas? 有任何想法吗?

EDIT

OK, so i figured out that the info in response is a string. 好的,所以我发现响应中的信息是一个字符串。 It must be converted to an INT using parseInt to be usable in the chart. 必须使用parseInt将其转换为INT,才能在图表中使用。 currently, I have 目前,我有

$("#awayTeam").change(function(){ 
        $.ajax({
        type: "POST",    
        data: "away=" + $("#awayTeam").val(),
        dataType: "json",
        url: "/getCharts.php",
        success: function(response){
              var asdf = [];
              asdf[0] = parseInt(response[0]);
              asdf[1] = parseInt(response[1]);
              asdf[2] = parseInt(response[2]);
              asdf[3] = parseInt(response[3]); 
              alert(asdf);

will have to write a function to make this cleaner. 将不得不编写一个函数来使这个更加干净。

I can't believe it, but I finally got it. 我不敢相信,但我终于明白了。 here is how I used an onchange method to stimulate a MYSQL query and have the Highchart display the result. 这是我使用onchange方法刺激MYSQL查询并让Highchart显示结果的方式。 The major problem was that the returned JSON array was a string that needed to be converted into an INT. 主要问题是返回的JSON数组是需要转换为INT的字符串。 The resultArray variable is then used in the data: portion of the highChart. 然后,在highChart的data:部分中使用resultArray变量。

$(function(){
    $("#awayTeam").change(function(){ 
        $.ajax({
        type: "POST",    
        data: "away=" + $("#awayRunner").val(),
        dataType: "json",
        url: "/getCharts.php",
        success: function(response){
              var arrayLength = response.length;
              var resultArray = [];
              var i = 0;
              while(i<arrayLength){
                  resultArray[i] = parseInt(response[i]);
                  i++;
              }            

In the PHP code, the array must be returned as JSON like this 在PHP代码中,数组必须像JSON这样返回

echo json_encode($awayRunner);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM