Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否可以获得两个不同的json输出_Php_Jquery_Ajax - Fatal编程技术网

Php 是否可以获得两个不同的json输出

Php 是否可以获得两个不同的json输出,php,jquery,ajax,Php,Jquery,Ajax,是否可以在一个页面结果集中获得两个json输出 我必须通过使用ajax将数据传递到另一个页面来填充数据网格和图表,并从单个mysql查询中获取两种类型的json结果集,当我尝试返回json时,它无法处理 下面是我的result.php代码,其中将生成json include('connection.php'); if (isset($_POST)) { $rep_date1 = $_POST['date1']; $date1 = date("Y-m-d", strtotime($

是否可以在一个页面结果集中获得两个json输出

我必须通过使用ajax将数据传递到另一个页面来填充数据网格和图表,并从单个mysql查询中获取两种类型的json结果集,当我尝试返回json时,它无法处理

下面是我的result.php代码,其中将生成json

include('connection.php');
if (isset($_POST)) {
    $rep_date1 = $_POST['date1'];
    $date1 = date("Y-m-d", strtotime($rep_date1));
    $rep_date2 = $_POST['date2'];
    $date2 = date("Y-m-d", strtotime($rep_date2));
    $sql = mysql_query("SELECT * FROM infra.prob_report WHERE prob_rept_date    BETWEEN '$date1' AND '$date2'");
    $rows = array();
    while ($row = mysql_fetch_assoc($sql)) {
        $nestedData = array();
        $nestedData[] = $row["rep_id"];
        $nestedData[] = $row["prob_rept_date"];
        $nestedData[] = $row["prob_equip_name"];
        $nestedData[] = $row["prob_rept_name"];
        $nestedData[] = $row["prob_desc"];
        $data[] = '<tr><td>'.$row["rep_id"].
        '</td><td>'.$row["prob_rept_date"].
        '</td><td>'.$row["prob_equip_name"].
        '</td><td>'.$row["prob_rept_name"].
        '</td><td>'.$row["prob_desc"].
        '</td></tr>';
        $point = array("label" => $row['prob_equip_name'], "y" => $row['rep_id']);
        array_push($data_points, $point);
    }
    echo json_encode($data); //json output populating data-grid
    echo json_encode($data_points); //json output populating chart
}
这是我的handle.php,我的处理脚本在这里运行

<script>
    $(document).ready(function() {

        $('#submit').click(function() {
            var name = $("#name").val();
            event.preventDefault();
            $.ajax({
                type: "POST",
                url: "new_prob_submit.php",
                data: {
                    'date1': $('#picker1').val(),
                    'date2': $('#picker2').val()
                },
                dataType: "json",
                success: function(data) {
                    $('#tbdy').html(data);
                    $.getJSON("result.php", function(result) {
                        var chart = new CanvasJS.Chart("chartContainer", {
                            data: [{
                                dataPoints: result
                            }]
                        });
                        chart.render();
                    });
                }
            });
        });
    });
</script>
只要做一件事

在php中

echo json_encode(
    array(
        'data' => $data, 
        'dataPoints' => $data_points
    )
); //json output populating data-grid and populating chart
在javascript中

success:function(result){ 
result.data;
result.dataPoints;
}

您可以将这两个数组都推入一个数组,然后回显最后一个输出数组。所以你有结果[0]来填充数据网格,结果[1]来填充图表。@ParthTrivedi你能详细解释一下吗,因为我必须使用同一组元素来呈现图表和数据网格。很简单,你明白了吗?@ParthTrivedi你的工作很好,得到了你的评论,非常感谢你的帮助!!!我认为你最好跟着“金丹”走answer@Balaji如果您觉得任何答案都有帮助,请用“true”标记。这样其他人可以通过“真”符号快速获得帮助。谢谢