dongzha5934 2018-04-13 07:04
浏览 50
已采纳

使用Ajax Codeigniter更新表或div数据

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

  • 写回答

1条回答 默认 最新

  • duandie0884 2018-04-13 07:36
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!