On the users table you could create a column called online
Name Type Length / Val
online tinyint 1
Then when the user logins in. Update that user online column to
1
Then on the a model function
public function countOnline() {
$this->db->where('online', '1');
$query = $this->db->get('users');
return $query->num_rows();
}
When the user logs out. You need to set that user online column to
0
Model
Filename: Users_model.php
<?php
class Users_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function countOnline() {
$this->db->where('online', '1');
$query = $this->db->get('users');
return $query->num_rows();
}
}
Controller
Filename: Dashboard.php
<?php
class Dashboard extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('users_model');
}
public function index() {
$data['online_users'] = $this->users_model->countOnline();
$this->load->view('header');
$this->load->view('dashboard', $data);
$this->load->view('footer');
}
}
To see new users online total you have to reload the page each time you want to see new users online.
There are scripts out there that can reload specific div's every few min.
Hope this gives you a idea
First this is controller , so put here this code in your controller function, than model function code put in your model file.
<?php
//controller function
function total_user()
{
/*load your model*/
$data=$this->model_name->total_users_number();
echo $data;
}
//model function
function total_users_number()
{
/*query*/
$sql = "SELECT COUNT(*)
FROM `users` where user_status='online' // here your table name
$query = $this->db->query($sql);
return $query->num_rows();
}
?>