Codeigniter count_all for pagination

I am trying to create pagination in codeigniter and I have work, but I have a little problem. It seems to load all the records in my database, not the selected ones that I want.

public function original_count() { $this->db->where('type', 'Original'); return $this->db->count_all("story_tbl"); } 

I know that what happens is that the last line redefines my previous statements. I can't seem to find a way around it. I tried just staight sql statement and then returned it, but I could not get this to work.

that was my expression ...

 SELECT COUNT(*) FROM story_tbl where type = 'Original'; 

Some help would be greatly appreciated! :)

+4
source share
4 answers

You can also use the built-in function num_rows() ...

 $query = $this->db->where('type', 'original')->get('story_tbl'); return $query->num_rows(); 
+8
source

CI has a built-in counting method

 count_all_results() 

Allows you to determine the number of rows in a particular Active Record query. Requests will accept Active Record constraints such as where (), or_where (), like (), or_like (), etc. Example:

http://ellislab.com/codeigniter/user-guide/database/active_record.html

 $total_count = $this->db->count_all_results('story_tbl', array('type' =>'Original')); 
+10
source

Try this first.

 $query = $this->db->where('tbl_field', 'value') ->get('your_table'); return $query->num_rows(); 

In addition, this Codeigniter has its own function, such as the following.

  $this->db->where('tbl_field', 'value') ->get('your_table'); return $this->db->count_all_results(); 

Or use it.

 return $this->db->count_all('your_table'); 
+3
source

Where does not work in the count_all condition .. you can use the method below to find out the total number of lines.

 public function count_all() { $this->db->select ( 'COUNT(*) AS `numrows`' ); $this->db->where ( array ( 'type' => 'Original' ) ); $query = $this->db->get ( 'story_tbl' ); return $query->row ()->numrows; } 
+1
source

Source: https://habr.com/ru/post/1481292/


All Articles