CodeIgniter COUNT with active record? CodeIgniter COUNT with active record? codeigniter codeigniter

CodeIgniter COUNT with active record?


You can use $this->db->count_all_results()

 public function get_my_orders() {    $user_data = $this->session->all_userdata();    $email = $user_data['user_email'];        //$this->db->select('*');// no need select as you only want counts        $this->db->from('order_details');        $this->db->where('email', $email);        //$this->db->group_by('order_no');//no need group_by as you only want counts        //$this->db->order_by('order_no', 'DESC');//no need order_by as you only want counts                    return $this->db->count_all_results();;}


Try changing your select line to look like this:

            $this->db->select('COUNT(*) as count');

Rather than having all fields accessible like they currently are, you will instead only have access to one variable called count. To keep all variables accessible and add the count in as well, use this instead:

            $this->db->select('*, COUNT(*) as count');

Feel free to change the lowercase name for count, I'm just using that as an example.


Check Codeigniter Manual

$query = $this->db->get();return  $query->num_rows();  //Return total no. of rows here

$query->num_rows(); will count the total active record return by your query.