[eluser]Unknown[/eluser]
Hello everyone
Im stuck for 2 days now , and i need your help.
I want to use ajax to update a div with the class .user-responce,every time the database column "status" is updated. I have a problem though, manipulating the json object created by the ajax controller and return it back at the view from jquery. This $data variable into controller makes my life hard at the moment and i keep getting errors.
I please for your help and thanks in advance
)
Here is my
index controller
Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home_page extends CI_Controller {
public function index() {
//$this->load->model('home_page_model');
//$data['query'] = json_encode($this->home_page_model->onlineUsers());
//$this->load->view('homepage',$data);
$this->load->view('homepage');
}
}
?>
This is my
Ajax_controller.php
Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ajax_controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function GetOnlineUsers() {
$this->load->model('home_page_model');
$data['query'] = json_encode($this->home_page_model->onlineUsers());
$this->load->view('homepage',$data);
}
}
?>
My
Model home_page_model.php
Code:
<?php
class home_page_model extends CI_Model {
function __construct() {
parent::__construct();
}
function onlineUsers() {
$this -> db -> select('user_name,status,thumb') -> from('users') -> where('status', 'Online');
$query = $this -> db -> get();
if ($query -> num_rows > 0) {
return $query -> result();
} else {
'There are no results';
}
}
}
?>
And finally my
View
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="cache-control" content="no-cache">
<meta charset="utf-8">
<title>Welcome!</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url()?>css/styles.css">
[removed][removed]
[removed]
setInterval(function()
{
$('.user-responce').load('<?php echo base_url()?>Ajax_controller/GetOnlineUsers');
}, 2000);
[removed]
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<div class="user-responce">
<?php
print_r($query);
?>
</div>
</div>
<p class="footer">
Page rendered in <strong>{elapsed_time}</strong> seconds
</p>
</div>
</body>
</html>