1

我只是 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>

提前致谢

4

3 回答 3

1

希望对你有帮助 :

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

您在控制器方法中的 $data 部分payments应该是这样的:

$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>
于 2018-07-19T09:48:36.650 回答
0

尝试SUMMySql 的功能为:

在模型中

public function total(){
$sql="SELECT SUM(bill_rpt ) as total_sum
FROM amilyar_bill
WHERE bill_status = 1";
return $this->db->query($sql);
}
于 2018-07-19T09:51:17.307 回答
0

您可以使用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>
于 2018-07-19T09:45:58.170 回答