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

时间:2018-07-19 09:41:38

标签: php codeigniter codeigniter-3 codeigniter-query-builder

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

预先感谢

3 个答案:

答案 0 :(得分:1)

希望这对您有帮助:

使用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>

答案 1 :(得分:0)

您可以使用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>

答案 2 :(得分:0)

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