Counting the number of results returned by a database query in Codeigniter

I am not very fortunate to discover when a database query in Codeigniter returns null results. I read the notes about the PHP count function well, but I'm not wiser!

I call the request / view as follows from the controller:

$data['result'] = $this->search_model->do_search(set_value('name')); $data['title'] = "Search results"; $this->load->view('search_view',$data); 

In the view, a result table is created for me OK, but when I try to catch an empty result, the counter always returns 1:

I tried if count(array($result)) and just if count($result)

So what is a good way to get an account? I am using Fedora 13 with PHP 5.3.3 on my dev laptop.

+6
php codeigniter
source share
6 answers

Take a look at $query->num_rows (<- clickable).

+11
source share

The best thing to do in your model is:

 $query = $this->db->something().... ... ... if ( $query->num_rows() > 0 ) { return $query->result(); } else { return FALSE; } 

Then in your controller or view, you will do the following:

 if ( !empty($my_db_result) ) { ...... } 

This process allows you to respond to a result based on the type of result. If the rows can be restored, this will return an array from which elements can be counted by the PHP count () function. Since the second block checks whether the result is empty (note that "FALSE" is considered empty), you will not encounter any problems (for example, when using the foreach loop), and you can specify what to do if there weren’t results.

+8
source share

Try if(isset($result) && count($result)) in your view file, then inside the if statement you can write the code you want to execute when the inserts in your db are greater than 0 ... good luck!

0
source share

If you put count($result) in an if , and then when it is executed, it returns only 1 .

You can try $query->num_rows() differently.

0
source share

for example, I am counting to show connected connected users and users connected to them

Database

for users, I add a table: [status] [int] [1]

users also have: [role] [varchar] [255]

Update statut to 1 (onligne) when checking login ()

 if($this->model_users->can_log_in($email,$pass)){ $update = array('status' => 1); $this->model_users->update_onligne($email,$update); redirect('main/members'); 

and model:

 public function update_onligne($email,$update){ $this->db->where('email',$email); $this->db->update('users',$update); return true; } 

Update offline status in log ()

Output controller:

 public function logout(){ $id = $this->session->userdata('id'); $update = array('status' =>0); $this->model_users->logout($id,$update); $this->session->sess_destroy(); redirect('main/login'); } 

Output Model:

 public function logout($id,$update){ $this->db->where('id',$id); $this->db->update('users', $update); return; } 

Count Onligne:

Controller:

 $data['admin_onligne'] = $this->model_users->count_onligne_admin(); $data['user_onligne'] = $this->model_users->count_onligne_users(); $this->load->view('template/page_left',$data); 

Model:

 public function count_onligne_admin(){ $query = $this->db->query('SELECT COUNT(status) AS enligneadmin FROM users WHERE status=1 AND role="admin"')->row_object(); return $query->enligneadmin; } public function count_onligne_users(){ $query = $this->db->query('SELECT COUNT(status) AS enligneuser FROM users WHERE status=1 AND role="etudiant"')->row_object(); return $query->enligneuser; } 

Viewer

 <span><?php echo $user_onligne ;?> User en ligne</span> <span><?php echo $admin_onligne ;?> Admin en ligne</span> 
0
source share

I fool it and it works for me

controller

 $id = $this->session->userdata('id'); if($this->model_users->if_user_dont_have_email($id)){ .... } 

Model Users

 public function if_user_dont_have_email($id){ $query = $this->db->query("SELECT email FROM users WHERE id='$id'"); if ($query->num_rows() > 0) { $row = $query->row_array(); if(empty($row['email'])){ return true; }else{ return false; } } } 
0
source share

All Articles