user3675089
user3675089

Reputation: 47

CodeIgniter Active Record - Get number of returned rows and get the sum

I'm new to CodeIgniter and Active Record in particular

How can I Get number of returned rows and get the SUM of two returned tables row counts.

I already have function to get row count like this

controller

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

model

 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();
    }

Upvotes: 1

Views: 1630

Answers (2)

podijobs
podijobs

Reputation: 86

It is not a big deal. You can do something like this. Assist PHP array_sum .This is php manual to array sum You already have the row counts

in your controller

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

in your view echo $count_both ;

Upvotes: 2

Babiker
Babiker

Reputation: 18798

If that's really your only goal, just execute one query:

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

Upvotes: 1

Related Questions