我只是Codeigniter的新手。我想在视图页面中显示此SQL查询的bill_rpt的总和。这是代码
这是我的型号
public function total() {
$sql="SELECT bill_rpt
FROM amilyar_bill
WHERE bill_status = 1";
return $this->db->query($sql);
}
这是我的控制器
public function payments($token=''){
$this->isLoggedIn();
$data = array(
// get data using email
'token' => $token,
'admin_info' => $this->model->getAdminInfo($this->session->userdata('email'))->row(),
'notifications' => $this->model->notification_admin($this->session->userdata('email'))->result_array(),
'notification' => $this->model->all_notification_admin($this->session->userdata('email'))->result_array(),
'total' => $this->model->total($this->session->userdata('email'))->result_array(),
);
if ($this->session->userdata('position_id') == 2) {
$this->load->view('includes/admin_header',$data);
$this->load->view('admin/reports/payments',$data);
} else {
$this->logout();
}
}
这是我的视图
<h4 class="pull-right">Total:</h4>
提前感谢
希望这将帮助您:
使用ciselect_sum()
获得总和
你的total
方法应该是这样的:
public function total()
{
$this->db->select_sum('bill_rpt');
$this->db->where('bill_status' , '1');
$query = $this->db->get('amilyar_bill');
return $query->row()->bill_rpt;
}
控制器payments
方法中的$data部分应该是这样的:
$data = array(
'token' => $token,
'admin_info' => $this->model->getAdminInfo($this->session->userdata('email'))->row(),
'notifications' => $this->model->notification_admin($this->session->userdata('email'))->result_array(),
'notification' => $this->model->all_notification_admin($this->session->userdata('email'))->result_array(),
'total' => $this->model->total()
);
在payments
视图部分中,使用$total,如下所示:
<h4 class="pull-right">Total: <?=$total;?></h4>
您可以使用Active class的num_rows()
方法。
$query->num_rows();
public function total(){
$sql="SELECT bill_rpt FROM amilyar_bill WHERE bill_status = 1";
$query = $this->db->query($sql);
return $query->num_rows();
}
在视图中,
<h4 class="pull-right">Total: <?php echo $total;?></h4>
尝试MySql的SUM
函数为:
在模型中
public function total(){
$sql="SELECT SUM(bill_rpt ) as total_sum
FROM amilyar_bill
WHERE bill_status = 1";
return $this->db->query($sql);
}