我特别擅长CodeIgniter和Active Record
如何获取返回行的数量并获得两个返回表行数的SUM。
我已经有了像这样的行计数的功能
控制器
function newsletter (){
$data = array();
$data['subscriber_count'] =$this->mod_contactus->count_subscriber();
$data['user_count'] =$this->mod_contactus->count_reg_users();
$this->load->view('admin/admin_newsletter',$data);
}
模型
public function count_subscriber() {
return $this->db->get("tbl_subscribers")->num_rows();
}
public function count_reg_users() {
return $this->db->get("tbl_customer_registration")->num_rows();
}
答案 0 :(得分:2)
这不是什么大问题。你可以做这样的事情。协助PHP array_sum
。这是php manual to array sum您已经拥有行数
function newsletter (){
$data = array();
$data['subscriber_count'] =$this->mod_contactus->count_subscriber();
$data['user_count'] =$this->mod_contactus->count_reg_users();
$data['count_both'] = array_sum($data);
$this->load->view('admin/admin_newsletter',$data);
}
在您的视图中echo $count_both ;
答案 1 :(得分:1)
如果这真的是你唯一的目标,那就执行一个查询:
$query = $this->db->query("
select
( select count(*) from table_1 ) +
( select count(*) from table_2 )
as total_num_row
from table_1
");
# $query->num_rows()