如何组合JSON结果

时间:2013-02-08 23:38:30

标签: php jquery json

何我可以结合JSON的结果?

问题是,在尝试将此结果组合在codeigniter上时,我得到一个错误致电成员的致命错误result_array()

$this->alerts
            ->select('products.id as productid, products.code as code, products.name, products.price, sum(purchase_items.quantity)')
            ->from('products');
            $this->alerts->join('purchase_items', 'products.id = purchase_items.product_id');
                $this->alerts->group_by("products.id");
            echo "Total Purchases";
            echo $this->alerts->generate();


 $this->alerts
            ->select('products.id as productid, products.code as code, products.name, products.price, sum(sale_items.quantity)')
            ->from('products');
            $this->alerts->join('sale_items', 'products.id = sale_items.product_id');           
                $this->alerts->group_by("products.id");
            echo "Total Sales";
            echo $this->alerts->generate();

结果为JSON:

{"sEcho":0,"iTotalRecords":210,"iTotalDisplayRecords":210,"aaData":[["522","E12345","BAMBOO SCARF","100.00","3"]],"sColumns":"productid,code,products.name,products.price,sum(sale_items.quantity)"}

{"sEcho":0,"iTotalRecords":210,"iTotalDisplayRecords":210,"aaData":[["522","E12345","BAMBOO SCARF","100.00","15"]],"sColumns":"productid,code,products.name,products.price,sum(sale_items.quantity),productid,code,products.name,products.price,sum(purchase_items.quantity)"}

从json获取数据的脚本:

$(document).ready(function() {
                $('#fileData').dataTable( {
                    "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
                    "sPaginationType": "full_numbers",  
                    "aaSorting": [[ 0, "desc" ]],
                    "bJQueryUI": true,
                    "sDom": '<"H"frlT><"clear">t<"clear"><"F"ip>',
                        //<"H"T><"clear">
                        //"sDom": '<"top"i>rt<"bottom"flp><"clear">',
                    'bProcessing'    : true,
                    'bServerSide'    : true,
                    'sAjaxSource'    : 'index.php?getdata',
                    'fnServerData': function(sSource, aoData, fnCallback)
                    {
                      $.ajax
                      ({
                        'dataType': 'json',
                        'type'    : 'POST',
                        'url'     : sSource,
                        'data'    : aoData,
                        'success' : fnCallback
                      });
                    },  
                    "oLanguage": {
                      "sSearch": "Filter: "
                    },
                    "aoColumns": [ 
                      null,
                      null,
                      null,
                      null,
                      null,
                      { "bSortable": false }
                    ]

                } );

            } );

1 个答案:

答案 0 :(得分:1)

不要立即回显响应,而是尝试将它们堆叠成一个数组,然后输出JSON。

<?php

//......

$result = arrray();

$result[] = $this->alerts->generate();
$result[] = $this->alerts2->generate();

echo json_encode($result);