简体   繁体   English

使用Ajax Codeigniter更新表或div数据

[英]Updating Table or div Data using Ajax Codeigniter

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作为服务器,并使用PHP Ajax进行脚本编写。

Openfire dumps data into MySQL Table. Openfire将数据转储到MySQL表中。 I retrive all the records using Codeigniter-PHP: 我使用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. 现在,我不想重新加载整个页面,而只是每5秒刷新一次表内容。

I am using DataTables . 我正在使用DataTables

I also thought I could pass json to my view, but I don't know how to update it every 5 seconds. 我还认为我可以将json传递给我的视图,但是我不知道如何每5秒更新一次。

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 : 您必须使javascript函数像这样:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM