Counter lines php codeigniter

I'm new to codeigniter, I want to read all rows from a database table, but the dose of the query does not return the exact number of rows. Here is a model

public function countRow(){ $query = $this->db->query("SELECT *,count(id) AS num_of_time FROM home"); // print_r($query->result()); return $query->result(); } 

and this is the controller

 public function countTotalrow(){ $data['query'] = $this->home_model->countRow(); } 

this is a view

 foreach ($num_of_time as $row){ <span><?php print_r($row->id);?></span> 
+11
source share
11 answers

You can use the helper function $query->num_rows()

Returns the number of rows returned by the request. You can use the following:

 $query = $this->db->query('SELECT * FROM my_table'); echo $query->num_rows(); 
+31
source

Use this code:

$this->db->where(['id'=>2])->from("table name")->count_all_results();

or

$this->db->from("table name")->count_all_results();

+11
source

This is what I did, that I solved the same problem. I solved this by creating a function that returns the result of the query this way:

 function getUsers(){ $query = $this->db->get('users'); return $query->result(); } 

// The above code can be in user_model or in any other model.

This allows me to use one function for the result and the number of rows returned.

Use this code below in your controller where you need a counter as well as an array of results ().

 //This gives you the user count using the count function which returns and integer of the exact rows returned from the query. $this->data['user_count'] = count($this->user_model->getUsers()); //This gives you the returned result array. $this->data['users'] = $this->user_model->getUsers(); 

Hope this helps.

+3
source

If you really want to read all the rows. You can use this in model functions:

 $this->db->select('count(*)'); $query = $this->db->get('home'); $cnt = $query->row_array(); return $cnt['count(*)']; 

It returns a single value, i.e. the number of rows

+1
source

replace the query inside your model function with

 $query = $this->db->query("SELECT id FROM home"); 

in sight:

 echo $query->num_rows(); 
+1
source

You can try this

  $this->db->where('field1',$filed1); $this->db->where('filed2',$filed2); $result = $this->db->get('table_name')->num_rows(); 
+1
source

To count all the rows in a table:

 echo $this->db->count_all_results('table_name'); 

To count selected rows from a table:

 echo $this->db->where('name', $name)->count_all_results('table_name'); 
+1
source

To count all the rows in a table:

controller:

 function id_cont() { $news_data = new news_model(); $ids=$news_data->data_model(); print_r($ids); } 

Model:

 function data_model() { $this->db->select('*'); $this->db->from('news_data'); $id = $this->db->get()->num_rows(); return $id; } 
+1
source
 public function number_row() { $query = $this->db->select("count(user_details.id) as number") ->get('user_details'); if($query-> num_rows()>0) { return $query->row(); } else { return false; } } 
+1
source

Try this :) I created my model to calculate all the results.

in library_model

 function count_all_results($column_name = array(),$where=array(), $table_name = array()) { $this->db->select($column_name); // If Where is not NULL if(!empty($where) && count($where) > 0 ) { $this->db->where($where); } // Return Count Column return $this->db->count_all_results($table_name[0]);//table_name array sub 0 } 

Your controller will look like this

 public function my_method() { $data = array( $countall = $this->model->your_method_model() ); $this->load->view('page',$data); } 

Then a simple library model call in your model

 function your_method_model() { return $this->library_model->count_all_results( ['id'], ['where], ['table name'] ); } 
0
source
 public function record_count() { return $this->db->count_all("tablename"); } 
0
source

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


All Articles