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>
casti cyber dev
source share