我正在尝试使用 json_encode 将一些数据回显到我的 ajax 调用,但我似乎只接收到一部分数据,特别是最后一条记录。在下面的查询中,当我$q->num_rows();
正确返回正确的行数时,6。但是,当我查看生成的 JSON 对象时,我看到它只有第 6 行的最后一对值。第 1 行的数据-5 无处可寻。
谁能看到我可能做错了什么?谢谢。
function myAjaxFunction(){
$muni = $this->input->POST('muni');
$q = $this->db->query("SELECT SaleYear AS Y, NewSaleType AS T, count(*) AS C, format((sum(SalePrice) / sum(Quantity1)),0) AS R FROM tblsales WHERE tblsales.SaleYear > 2007 AND tblsales.Quantity1 > 2000 AND (tblsales.NewSaleType = 'Industrial') AND tblsales.Municipality = '".$muni."' GROUP BY T,Y ORDER BY T,Y");
$json_SaleYear = array();
$json_AvrgRate = array();
foreach ($q->result_array() as $row){
$json_SaleYear = $row['Y'];
$json_AvrgRate = $row['R'];
}
$json['Y'] = $json_SaleYear;
$json['R'] = $json_AvrgRate;
echo json_encode($json);
exit;
}