I am trying to create a page where it shows the current chats done by clients.
Now I am using Openfire as Server, and PHP Ajax for scripting.
Openfire dumps data into MySQL Table.
I retrive all the records using Codeigniter-PHP:
public function get_chats()
{
$this->db->select('*');
$this->db->from('ofMessageArchive');
$query = $this->db->get();
$result = $query->result();
$this->data['messages'] = $result;
$this->data['subview'] = 'dashboard/test';
$this->load->view('_layout_main', $this->data);
}
Now on my view I have table :
<table class="table table-striped table-bordered table-hover" >
<thead>
<th>From</th>
<th>To</th>
<th>Message</th>
<th>Time</th>
</thead>
<tbody>
<?php if(count($messages)): foreach($messages as $key => $message): ?>
<tr>
<td><?php $users = explode("__", $message->fromJID); echo $users[0];?></td>
<td><?php $tousers = explode("__", $message->toJID); echo $tousers[0]; ?></td>
<td><i class="material-icons">play_circle_filled</i>Message</td>
<td><?php echo $date->format('d-m-y H:i:s'); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
Now I don't want to reload the whole page instead just refresh the table contents every 5 seconds.
I am using DataTables.
I also thought I could pass json to my view, but I don't know how to update it every 5 seconds.
public function get_chats()
{
$this->db->select('*');
$this->db->from('ofMessageArchive');
$query = $this->db->get();
$result = $query->result();
if (count($result)) {
$response = json_encode($result);
}
else
{
$response = false;
}
echo $response;
}
Also this is going to send new and older messages.
So I only want to append news messages to table old message should be not repeated
Your backend function :
public function get_chats()
{
$this->db->select('*');
$this->db->from('ofMessageArchive');
$query = $this->db->get();
$result = $query->result();
if (count($result)>0) {
$response['status']= true;
$response['messages']= $result;
$response['date']= date("d-m-y H:i:s", strtotime($your-date-here));
}
else
{
$response['status']=false;
}
echo json_encode($response);
}
You have to make javascript function like this :
<script>
function myAJAXfunction(){
$.ajax({
url:'your url here',
type:'GET',
dataType:'json',
success:function(response){
console.log(response);
var trHTML='';
if(response.status){
for(var i=0;i<response.messages.length;i++){
users =response.messages[i]fromJID.slice('--');
touser=response.messages[i]toJID.slice('--');
trHTML=trHTML+
'<tr>'+
'<td>'+users[0]+'</td>'+
'<td>'+touser[0]+'</td>'+
'<td><i class="material-icons">play_circle_filled</i>Message</td>'+
'<td>'+response.date+'</td>'+
</tr>';
}
$('tbody').empty();
$('tbody').append(trHTML);
}else{
trHTML='<tr><td colspan="4">NO DATA AVAILABLE</td></tr>';
$('tbody').empty();
$('tbody').append(trHTML);
}
},
error:function(err){
alert("ERROR LOADING DATA");
}
});
}
// calling above ajax function at every 5 seconds
setInterval(myAJAXfunction,5000);
</script>
Related
i tried to use print_r($list) but the result array().There is no element in the array.I want to show data form my table.there is user table, job table and industry table.Can anybody suggest me where is mistake.
This is my model function get_data_by_query used in controller
public function get_data_by_query($qry)
{
$query = $this->db->query($qry);
return $query->result();
}
This is my controller where i am verifying email, phone number.
public function Dashboard()
{
if ($this->Common_model->Is_User_Logged())
{
$condition='';
$data['email_verified']=self::IsEmailVerified();
$data['phone_verified']=self::IsPhoneVerified();
$userdata=$this->session->userdata('user_logged_in');
$data['user_email']=$userdata['email'];
//print_r($data);
$res=$this->Common_model->get_data_by_query("SELECT * FROM user WHERE user_id={$userdata['id']} limit 1");
//print_r($res);
$data['user_skills'] = $res[0]->user_skills;
//print_r($data);
$arr = array($res[0]->user_skills);
//print_r($arr[0]);
//exit;
$condition.=" and FIND_IN_SET(job_title,'$arr[0]')";
print_r($condition);
$sql_get="SELECT j.*,j.job_industry,i.industry_title
FROM job j
LEFT JOIN industry i on i.industry_id=j.job_industry
WHERE job_status=1 ".$condition." ";
//print_r($sql_get);
$data['list']=$this->Common_model->get_data_by_query($sql_get);
//print_r($data['list']);
$data['user_phone']=$res[0]->user_mobile;
$this->load->view('user/header');
$this->load->view('user/home', $data);
$this->load->view('user/footer');
}
else
{
redirect('user');
}
}
This is my view
<?php
if(!empty($list)>0)
{
foreach ($list as $row)
{
?>
<tr>
<td><?php echo $row->job_role;?></td>
<td><?php echo $row->industry_title;?></td>
<td><?php echo $row->job_vacancy;?></td>
<td><?php echo $row->job_keyword;?></td>
<!--<td><?php echo $row->job_type;?></td>-->
</tr>
<?php
} }
?>
what is the reason that $list is empty array although i have data in my table. I will be thankful for you.
After a lot of effort, i found that there is no problem in above code. The problem is in tje SQL query. I am trying to resolve that query.
I have a table that store the data of ID, ID Login, Name, etc. But i just want to show the data based on ID Login
Here My Controller :
function index(){
$data['hasil'] = $this->M_user_lapor->index_model();
$this->load->view('v_user_lapor/index', $data);
}
My Model :
function index_model(){
$baca = $this->db->query('select * from user_lapor');
if($baca->num_rows() > 0){
foreach ($baca->result() as $data){
$hasil[] = array(
'id_login'=>$data->id_login,
'id_lapor'=>$data->id_lapor,
'nm_unit'=>$data->nm_unit,
'pic_1'=>$data->pic_1,
'pic_2'=>$data->pic_2,
'ip_wan'=>$data->ip_wan,
'ip_lan'=>$data->ip_lan,
'prov'=>$data->prov,
'icn_sid'=>$data->icn_sid,
'tlkm_sid'=>$data->tlkm_sid,
'status'=>$data->status,
);
}
return json_encode ($hasil);
}else{
return false;
}
}
View :
<tbody>
<?php
if ($hasil){
$no = 1;
$array = json_decode($hasil, true);
foreach($array as $data) {
?>
<tr>
<td class="text-center"><?php echo $no++;?></td>
<td><?php echo $data['nm_unit'];?></td>
<td><?php echo $data['pic_1'];?></td>
<td><?php echo $data['pic_2'];?></td>
<td><?php echo $data['ip_wan'];?></td>
<td><?php echo $data['ip_lan'];?></td>
<td><?php echo $data['prov'];?></td>
<td><?php echo $data['icn_sid'];?></td>
<td><?php echo $data['tlkm_sid'];?></td>
</tr>
<?php
}
}
?>
</tbody>
As you can see, there is id_login inside my model, and i want to show the table data based on it, hopefully somebody can help me because i'm just using the codeigniter, thnaks
I solved this by myself, lol. I just pass the id_login value from session, so i add this to my controller :
function index(){
$id_login = $this->session->id_login;
$data['hasil'] = $this->M_user_lapor->index_model($id_login);
$this->load->view('v_user_lapor/index', $data);
}
And call it to my model :
function index_model($id_login){
$baca = $this->db->query('select * from user_lapor where id_login='.$id_login);
if($baca->num_rows() > 0){
foreach ($baca->result() as $data){
$hasil[] = array(
'id_login'=>$data->id_login,
'id_lapor'=>$data->id_lapor,
'nm_unit'=>$data->nm_unit,
'pic_1'=>$data->pic_1,
'pic_2'=>$data->pic_2,
'ip_wan'=>$data->ip_wan,
'ip_lan'=>$data->ip_lan,
'prov'=>$data->prov,
'icn_sid'=>$data->icn_sid,
'tlkm_sid'=>$data->tlkm_sid,
'status'=>$data->status,
);
}
return json_encode ($hasil);
}else{
return false;
}
}
Im fetching data from a table to another table that will be shown in my dashboard page for a user after loggin in.
But there is a problem with the indexes, i got this error:
Here is the line error:
Here is my code:
My view file ("usuario"):
<thead>
<th>id</th>
<th>User</th>
<th>Subject</th>
<th>Grade</th>
<th>Date</th>
</thead>
<tbody>
<?php
if (count($records) > 0 && $records != false) {
foreach($records as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['User']."</td>
<td>".$record['name']."</td>
<td>".$record['grade']."</td>
<td>".$record['date']."</td>
</tr>";
}
}
?>
</tbody>
</body>
</html>
My controller file ("login"):
public function home(){
$data['record']=$this->m_login->getDetails();
$this->load->view('usuario',$data);
}
My model file ("m_login"):
public function getDetails()
{
$st=$this->db->SELECT('cursadas.*, usuarios.name as usuarios, materias.name as materias_name')->from('cursadas')
->join('usuarios','usuarios.id=cursadas.user_id')
->join('materias','materias.id=cursadas.subject_id')
->WHERE('cursadas.user_id=',$this->session->userdata['id'])
->get()->result_array();
return $st[0];
}
You have the variable $records on view but not on controller
Change
$data['record'] = $this->m_login->getDetails();
To
// add this array() just in case no results found
$data['records'] = array();
$data['records'] = $this->m_login->getDetails();
$this->load->view('usuario', $data);
Another way is on controller
$results = $this->m_login->getDetails();
$data['records'] = array();
if ($results) {
foreach ($results as $result) {
$data['records'][] = array(
'id' => $result['id'],
'User' => $result['User'],
'name' => $result['name'],
'grade' => $result['grade'],
'date' => $result['date']
);
}
}
$this->load->view('usuario',$data);
View
<?php if ($records) {?>
<?php foreach($records as $record) {?>
<tr>
<td><?php echo $record['id'];?></td>
<td><?php echo $record['User'];?></td>
<td><?php echo $record['name'];?></td>
<td><?php echo $record['grade'];?></td>
<td><?php echo $record['date'];?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td>No Results Found</td>
</tr>
<?php } ?>
Wrong array index in your controller.
Change this
$data['record']=$this->m_login->getDetails();
to
$data['records']=$this->m_login->getDetails();
result_array() return returns result with array so you need to it like this -
if (count($record) > 0 && $record != false) {
foreach($record as $rec){
echo "<tr>
<td>".$rec['id']."</td>
<td>".$rec['User']."</td>
<td>".$rec['name']."</td>
<td>".$rec['grade']."</td>
<td>".$rec['date']."</td>
</tr>";
}
}
Please check this from above code and comment if you have any problem
someone please help me, I use the CodeIgniter with mvp procedure. I have a database like this:
is it possible if I want to display data in a table view like this?
I can only show you on a date, when retrieving data at another date id_status everything is not working.
controller when one date only.
public function status()
{
$tanggal1 = '2017-03-28';
$data['status'] = $this->M_Reports->status($tanggal1);
$this->load->view('homepage/template');
$this->load->view('report/status', $data);
}
model :
public function status($tanggal1) {
$sql = "SELECT operasional.id_ops, operasional.id_status, operasional.tanggal, peralatan.nm_peralatan FROM operasional INNER JOIN peralatan ON operasional.id_peralatan = peralatan.id_peralatan WHERE tanggal='$tanggal1' ORDER BY id_ops";
return $this->db->query($sql)->result();
}
And view :
<?php
$start = 0;
foreach($status as $data) {
?>
<tr>
<td><?php echo ++$start ?> </td>
<td><?php echo $data->nm_peralatan ?></td>
<td><?php echo $data->id_status ?></td>
<td><?php echo $data->tanggal ?></td>
</tr>
<?php
}
?>
You model function should be like this
public function status($top_of_date)
{
// This will get all dates
$dates=$this->db->distinct()->select('tanggal')->from('operasional')->get()->result_array();
// This will get all patients / peralatan
$patients=$this->db->distinct()->select('id_peralatan')->from('operasional')->get()->result_array();
// Loop through peralatan
for($i=0;$i<count($patients);$i++)
{
$st=$this->select('id_peralatan')->from('operasional')->WHERE('id_peralatan',$patients[$i])->get()->result_array();
// Start Creating an array for this index
$data[$i]=array(
'id_peralatan'=>$st[0]
);
// loop through dates
for($j=0;$j<count($dates);$j++)
{
//if date is greater than top of date
if($dates[$i]>$top_of_date)
{
$st=$this->db->select('id_status')->from('operasional')->WHERE('tanggal',$dates[$i])->get()->result_array();
// Create index and save the status
$data[$i]['top_of_date']=$st[0]['id_status'];
}
elseif($dates[$i]<$top_of_date)
{
$st=$this->db->select('id_status')->from('operasional')->WHERE('tanggal',$dates[$i])->get()->result_array();
// Create index and save the status
$data[$i][$dates[$i]]=$st[0]['id_status'];
}
}
}
return $data;
}
Now from your controller you only have to send top of date. like;
public function status()
{
$tanggal_top = '2017-03-28';
$data['status'] = $this->M_Reports->status($tanggal_top);
$this->load->view('homepage/template');
$this->load->view('report/status', $data);
}
So, i've been using codeigniter for a while and I've never had an issue running a query in my model and returning it to my controller which then passes it into my view where I access it as an object, like so:
MODEL (somemodel)
function getdata()
{
$query = $this->db->get('sometable');
return $query;
}
CONTROLLER (somecontroller)
function controldata()
{
$this->load->model('somemodel');
$data['dbdata'] = $this->somemodel->getdata();
$this->load->view('someview',$data);
}
VIEW (someview)
<?php
foreach($dbdata->result() as $row) {
echo $row->id;
echo $row->name;
echo $row->whatever;
echo "<br />";
}
?>
But now for whatever reason in 2.0 it's making use $dbdata->result_array() as $row instead.
Basically it's returning the results as an array instead.
It doesn't make any sense, has something changed in the new version?
Thanks,
So here's my code:
MODEL (admin_model)
function show_allproducts($sort,$order,$limit,$offset)
{
$this->db->select('products.id,productcategories.categoryname,products.name,products.internetspecial,products.added,products.modified');
$this->db->from('products');
$this->db->join('productcategories','products.productcategories_id = productcategories.id');
$this->db->order_by($sort,$order);
$this->db->limit($limit,$offset);
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query;
}
else
{
return FALSE;
}
}
CONTROLLER (admin)
function show_allproducts()
{
$data['title'] = 'All Products';
$this->load->library('pagination');
$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/admin/show_allproducts';
$config['total_rows'] = $this->db->get('products')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 5;
$config['full_tag_open'] = '<div class="paggination right">';
$config['full_tag_close'] = '</div>';
$config['prev_link'] = '< prev';
$config['next_link'] = 'next >';
$config['cur_tag_open'] = '<a class="active">';
$config['cur_tag_close'] = '</a>';
$this->pagination->initialize($config);
$this->load->model('admin_model');
$data['tabledata'] = $this->admin_model->show_allproducts('name','ASC',$config['per_page'],$this->uri->segment(3));
$data['nav'] = 'admin/pagesections/nav';
$data['maincontent'] = 'admin/pages/show_allproducts';
$this->load->view('admin/template',$data);
}
VIEW (show_all_products)
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<?php if($tabledata) { ?>
<tr>
<th>ID</th>
<th>CATEGORY</th>
<th>NAME</th>
<th>ADDED</th>
</tr>
<?php foreach($tabledata->result() as $row) { ?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->categoryname; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->added;) ?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td>NO DATA</td>
</tr>
<?php } ?>
</table>
Unless you specify $dbdata->result_array() as $row , It wont return the results as array. The same code which you've posted must be working in CI 2.0.
If not, may be the problem with $this->db->get('sometable').
Try with $this->db->query('select * from sometable'). But as far as i know, both are same...
Ok folks, no lie, I found the freaking problem.
Looking at the fourth td where $row->added; is at, there's a close parantheses at the end of it that was causing 4 days of mayhem.
Kindest regards to all those who tried to help. Sometimes it doesn't matter how many eyes are on it I guess.