【问题标题】:Highcharts does not display when a line of PHP header('location...') is added添加一行 PHP header('location...') 时,Highcharts 不显示
【发布时间】:2014-03-03 15:10:50
【问题描述】:

我正在使用 Highcharts 显示包含房间日期/时间和温度的图表。

用于生成图表的 javascript 位于用户可以查看的 temperature.php 文件中,并且 javascript 将从 dataSorter.php 文件中获取数据,该文件包含 SQL 查询以从 MySQL 检索图表的结果显示。

Javascript 在 temperature.php 中生成图表:

<script type="text/javascript">
    $(document).ready(function() {
        var options = {
            chart: {
                renderTo: 'container',
                type: 'line',
                marginRight: 130,
                marginBottom: 50
            },
            title: {
                text: 'Temperature vs. Time',
                x: -20 //center
            },
            subtitle: {
                text: '',
                x: -20
            },
            xAxis: {
                categories: []
            },
            yAxis: {
                title: {
                    text: 'Degrees Celcius'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function() {
                        return '<b>'+ this.series.name +'</b><br/>'+
                        this.x +': '+ this.y;
                }
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -10,
                y: 100,
                borderWidth: 0
            },
            series: []
        }

        $.getJSON("dataSorter.php", function(json) {
            options.xAxis.categories = json[0]['data'];
            options.series[0] = json[1];
            chart = new Highcharts.Chart(options);
        });
    });
    </script>  

此时,我在 temperature.php 中生成了 4 个下拉列表,其中包含 From Date、To Date、From Time 和 To Time。这将允许用户选择他们希望看到图表生成的范围。 (例如 2014-01-20 00:00:00 到 2014-01-21 22:00:00)。一个按钮 onclick 将激活该功能:

        if(isset($_POST['sort'])){
        $from=$_POST['SDate'];
        $to=$_POST['EDate'];
        $sTime=$_POST['STime'];
        $eTime=$_POST['ETime'];
        $start=$from." ".$sTime;
        $end=$to." ".$eTime;

        header('Location: dataSorter.php?start='.$start.'&end='.$end.'');
        }
        ?>
  1. $from = 开始日期
  2. $to = 结束日期
  3. $sTime = 开始时间
  4. $eTime = 结束时间
  5. $start = 结合 $from 和 $sTime 以获得开始日期/时间
  6. $end = 结合 $to 和 $eTime 以获得结束日期/时间

dataSorter.php 代码如下:

<?php
$con = mysql_connect("localhost","root","password");

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

mysql_select_db("scsense", $con);

if(isset($_GET['start'])){
$start = $_GET['start'];
$end = $_GET['end'];

$sth = mysql_query("SELECT * FROM scsenseinfo WHERE roomID='501' AND (dateTime BETWEEN '$start' AND '$end') ORDER BY recordID");
$rows = array();
$rows['name'] = 'DateTime';
while($rr = mysql_fetch_assoc($sth)) {
$rows['data'][] = $rr['dateTime'];
}

$sth = mysql_query("SELECT * FROM scsenseinfo WHERE roomID='501' AND (dateTime BETWEEN '$start' AND '$end') ORDER BY recordID");
$rows1 = array();
$rows1['name'] = 'RoomTemperature';
while($r = mysql_fetch_array($sth)) {
$rows1['data'][] = $r['roomTemp'];
}

$result = array();
array_push($result,$rows);
array_push($result,$rows1);

$help = print json_encode($result, JSON_NUMERIC_CHECK);

mysql_close($con);
}

else{

$sth = mysql_query("SELECT * FROM (SELECT * FROM scsenseinfo WHERE roomID='501' ORDER BY recordID DESC LIMIT 5) AS tbl ORDER BY tbl.recordID ASC");
$rows = array();
$rows['name'] = 'DateTime';
while($rr = mysql_fetch_assoc($sth)) {
$rows['data'][] = $rr['dateTime'];
}

$sth = mysql_query("SELECT * FROM (SELECT * FROM scsenseinfo WHERE roomID='501' ORDER BY recordID DESC LIMIT 5) AS tbl ORDER BY tbl.recordID ASC");
$rows1 = array();
$rows1['name'] = 'RoomTemperature';
while($r = mysql_fetch_array($sth)) {
$rows1['data'][] = $r['roomTemp'];
}

$result = array();
array_push($result,$rows);
array_push($result,$rows1);


$help = print json_encode($result, JSON_NUMERIC_CHECK);

mysql_close($con);
}
//header('Location: L501TempSorter.php');

?>

如果我有 header('Location: L501TempSorter.php');未注释,图表不显示任何东西,即使在页面加载时,也没有单击按钮对日期进行排序。如果它被注释,图表会在加载时显示,但单击按钮以对日期进行排序会导致 dataSorter.php 并停留在页面上,该页面仅显示包含排序日期的数组。我真的需要帮助,提前谢谢你!

【问题讨论】:

    标签: javascript php mysql json highcharts


    【解决方案1】:

    当你已经打印了一些东西时,你不能使用header,就像你使用//header('Location: L501TempSorter.php');一样。

    如果您尝试将print 的输出保存到变量$help,则需要改用sprint。 Print 将始终输出到输出缓冲区并始终返回 1。对于任何不以 svs 开头的打印函数都是如此,函数名称中 print 之前的 s 始终表示它将返回结果字符串输出它。

    为了解决您的数据不显示的问题,请发布按钮的onlcick代码。应该是类似的ajax调用

    <script>
    $.getJSON('dataSorter.php', {
         sort:  'sortValue',
         STime: /* get start value */,
         ETime: /* get end value */,
         SDate: /* get start value */,
         EDate: /* get end value */
    }, function(data) {
         options.xAxis.categories = json[0]['data'];
         options.series[0] = json[1];
         chart = new Highcharts.Chart(options);
         /* or anything that updates your chart */
    });
    </script>
    

    在执行 onClick 函数时运行的 php 脚本。您可能应该对您的Location: 进行部分网址编码

    <?php
    header('Location: dataSorter.php?start='.urlencode($start).'&end='.urlencode($end));
    

    虽然我不明白你为什么在这里使用header('Location: ...');?你已经在服务器上,为什么告诉客户端浏览器加载一个不同的位置只是include你的脚本应该现在运行。

    【讨论】:

      【解决方案2】:

      如果您仅将此脚本用于返回 json,那么您使用位置的目的是什么?当您在 javascirpt 中加载 json 时,您会运行整个 php 脚本,因此返回 json 您会被重定向到其他位置,因此 json 不会在 javascript 中加载。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-10
        • 2012-11-30
        • 1970-01-01
        • 2016-09-11
        • 2014-03-14
        • 2012-08-17
        • 2016-12-07
        相关资源
        最近更新 更多