从控制器获取总和值以查看

时间:2014-10-26 21:51:00

标签: mysql codeigniter sum

这是我的控制器功能

function assignPartner($id){

    $data['projects']=$this->admin_model->getProfData($id,'id','project')->result();
    $data['partners']=$this->admin_model->getTableData('partner')->result();
    $data['budge']=$this->db->query('SELECT SUM(amount) FROM budge WHERE project_id="'.$id.'"')->result();
    //print_r($data);exit;
    $this->load->view('projectheader',$data);
    $this->load->view('assignpartner',$data);
    $this->load->view('projectfooter');

}

这是我的观看页面

<?php echo  $budge[0]->amount;?>

1 个答案:

答案 0 :(得分:2)

您需要为总和添加别名。如果您在视图中使用var_dump $ budge,您将看到实际名称 - 应该是混乱的东西,如:

    $budge[0]->SUM(amount).

将SQL更改为..

    SELECT SUM(amount) AS amount FROM budge WHERE project_id=