CodeIgniter中未定义的表数据错误

时间:2013-09-20 16:26:59

标签: ajax codeigniter jquery

我在CodeIgniter中得到Undefined table data error并且无法找出原因。当我第一次尝试从CI网站回显示例表时,一切正常:

function ajaxAvgSalePriceTable(){
$this->load->library('table');
$query = array(
         array('Name', 'Color', 'Size'),
         array('Fred', 'Blue', 'Small'),
         array('Mary', 'Red', 'Large'),
         array('John', 'Green', 'Medium')   
         );
echo $this->table->generate($query);
}  

但是,当我尝试使用自己的查询时,出现Undefined table data错误。以下是导致错误的代码:

function ajaxAvgSalePriceTable(){
    $this->load->library('table');
    $muni = $this->input->POST('muni');
    $query = "SELECT SaleYear AS Y, NewSaleType AS T, count(*) AS C, tblsales.Municipality AS M, 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,M,Y ORDER BY T,M,Y";
    echo $this->table->generate($query);
}  

任何人都可以看到我可能做错了吗?我已经单独测试了这个查询,它在其他地方工作得很好,只是不在这里工作。

1 个答案:

答案 0 :(得分:1)

将此更改为以下内容, 你忘了从查询中获取数据。

function ajaxAvgSalePriceTable(){
    $this->load->library('table');
    $muni = $this->input->POST('muni');
    $query = "SELECT SaleYear AS Y, NewSaleType AS T, count(*) AS C, tblsales.Municipality AS M, 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,M,Y ORDER BY T,M,Y";

$res = $this->db->query($query);
$data = $res->result_array();

    echo $this->table->generate($data);
}