如何在视图页面中显示总数(Codeigniter)

How to display total in view page (Codeigniter)

我刚接触 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>

提前致谢

您可以使用 num_rows() Active Class 方法。

$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>

希望对您有所帮助:

用ci select_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>

尝试 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);
}