【问题标题】:How can I create real-time count in codeigniter using ajax?如何使用 ajax 在 codeigniter 中创建实时计数?
【发布时间】:2019-09-30 06:33:43
【问题描述】:

这是我的观点:

<ul class="users-list clearfix">
                <?php if(!empty($adminslist)){
                    foreach($adminslist as $v){
        ?>
                    <li class="selectVendor" id="<?=$v['admin_id'];?>" title="<?=$v['fname'];?>">
                      <img src="<?=base_url('/assets/uploads/' . $v['img']);?>" alt="<?=$v['fname'];?>" title="<?=$v['fname'];?>">
                      <a class="users-list-name" href="#"><?=$v['fname'];?></a>
                    </li>
                <?php }?>

               <?php }else{?>
                <li>
                   <a class="users-list-name" href="#">No Admin Find...</a>
                 </li>
                <?php } ?>


              </ul>

这是我的 mysql 部分,我在此使用:-

<?php

                        $this->load->model(['OuthModel','UserModel']);

                        $list = $this->UserModel->adminList();
                        foreach ($list as $admin) {
                            $this->db->select('count(*) as count');
                            $this->db->from("chat");
                            $this->db->where("receiver_id", $this->session->userdata('id'));
                            $this->db->where("chat_status", "1");
                            $this->db->where("sender_id", $admin['id']);
                            $result1 = $this->db->get();
                            $count = $result1->result_array();
                            $final=$count[0]['count'];
                            if($this->OuthModel->Encryptor('decrypt', $v['admin_id']) == $admin['id']) {
                            ?>
                              <span class="label label-pill" style="border-radius:10px;color: black;"><?php echo $final?></span>
                            <?php }
                        }
                      ?>

我没有为实时计数尝试任何 ajax 代码我尝试了所有没有的 ajax 代码。行数和呼叫,它给了我所有的计数,我想要特定的管理员/用户计数或通知行。并且在 php 代码中它运行成功但没有给出实时计数。

这是我使用的 ajax:-

$(document).ready(function(){
 function load_unseen_notification(view = '')
 {

  $.ajax({
   url:"<?php echo base_url();?>Notify/index",
   method:"POST",
   data:{"view":view},
   dataType:"json",
   success:function(data)
   {
      $('.dropdown-menu').html(data.notification);
      $('.count').show();
      if(data.unseen_notification > 0)
      {    
         $('.count').html(data.unseen_notification);
      }  else if(data.unseen_notification == ''){
         $('.count').hide();
      }
   }
  });
 }
 load_unseen_notification();
 $(document).on('click', '.dropdown-toggle', function(){
  $('.count').html('');
  load_unseen_notification('yes');
 });
 setInterval(function(){
 load_unseen_notification();
 }, 1000);
 });

【问题讨论】:

  • 在变量中获取 ajax 响应并在 ajax 中使用异步
  • 以及我如何调用它,因为我必须返回 id 并且我的 id 处于 foreach 条件并且它给了我最后一个 id

标签: php ajax codeigniter


【解决方案1】:

尝试使用Sockets。如果我猜对了,那么您所追求的是一个连续的数据获取功能。你真的应该使用 websocket。

【讨论】:

    猜你喜欢
    • 2014-03-28
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 2017-11-16
    相关资源
    最近更新 更多