【问题标题】:The count of user is visible on badge when I visit the page. (Codeigniter)当我访问该页面时,用户数会显示在徽章上。 (代码点火器)
【发布时间】:2021-04-03 08:48:21
【问题描述】:

这里是新手。我的目标是当我访问该页面时,它会自动显示用户数,并将显示在我的徽章上。我提供了一个截图以获得更好的输出。提前谢谢你。

https://streamable.com/w74ori这是我现在的情况。 (我需要单击带有表格的按钮模式才能在我的徽章中显示值计数)

https://prnt.sc/w9kqxh 这是我的目标。 (它在我访问页面后显示,没有点击带有表格的按钮模式)

查看

<div class="tab-content" id="custom-tabs-two-tabContent">
              
              <div class="tab-pane fade show active" id="custom-tabs-two-all" role="tabpanel" aria-labelledby="custom-tabs-two-all-tab">
                     <table class="table">
                            <thead class="">
                                <tr>
                                <th scope="col"><i class="fas fa-hashtag"></i></th>
                                <th scope="col"><i class="fas fa-mobile-alt mr-2"></i>UUID</th>
                                <th scope="col">Full Name</th>
                                 <th scope="col"><i class="fas fa-envelope-open-text mr-2"></i>Email</th>
                                <th scope="col"><i class="fas fa-phone-alt mr-2"></i>Contact Number</th>
                                <th scope="col"><i class="fas fa-at mr-2"></i>Username</th>
                                <th scope="col">Level</th>
                                <th scope="col">balance</th>
                                <th scope="col">&emsp;&emsp;&nbsp;Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                
                               <?php 
                               foreach($result as $rows) {   $uuid = $rows->uuid;
                               $userID = $rows->userID; ?>     
                                   <?php if($rows->uuid===$_SESSION['uid']): ?>     
                                <tr>
                                <th><?php echo $rows->userID;  ?></th>
                                <td><?php echo $rows->uuid; ?></td>
                                <td><?php echo $rows->firstname; ?>&nbsp;<?php echo $rows->lastname; ?></td>
                                <td><?php echo $rows->email; ?></td>
                                <td><?php echo $rows->mobile; ?></td>
                                <td><?php echo $rows->username; ?></td>
                                <td><?php echo $rows->account_type; ?></td>
                                <td><?php echo $rows->currentPoints; ?></td>
                                <td>
                                   
                                        <button data-id="<?php echo $rows->userID; ?>" class=" fundTable btn btn-success btn-sm text-bold " type="button"  data-toggle="modal" data-target="#fundModal">
                                            <i class="fas fa-hand-holding-usd mr-1"></i> <?php echo $rows->userID; ?>FUND
                                        </button>
                                        
                                        
                                        <button data-id="<?php echo $rows->userID; ?>" class=" allTable btn btn-danger btn-sm text-bold" type="button"  data-toggle="modal" data-target="#editModal">
                                            &nbsp;&nbsp;&nbsp;<i class="fas fa-users"></i>
                                            <span data-id="<?php echo $rows->userID; ?>" class="badge badge-warning navbar-badge"></span>
                                            
                                        </button>   
                                 
                                        
                                        
                                
                                        
                               </td>
                               </tr>
                                     <?php else: ?>
                                                  
                                            <?php endif;?>
                                     <?php } ?>
                            </tbody>
                     </table>
   </div>
 <span class="badge badge-warning navbar-badge">2</span>  //this is where the total value should be displayed.                                           
    </button> 

型号

public function view($userID = 0){
            $this->db->select("*");
            $this->db->from("users");
            $this->db->where( "uuid=".$userID);
            $query = $this->db->get();
            return $result = $query->result();
            
        }

控制器

public function view()
    {
        
        if ($this->input->is_ajax_request()) {
            $view_id = $this->input->post('view_id');
            
            if ($post = $this->networks->view($view_id)) {
                $data = array('responce' => 'success', 'post' => $post);
              
            } else {
                $data = array('responce' => 'error', 'message' => 'failed to fetch record');
            }
            echo json_encode($data);
        } else {
            echo "No direct script access allowed";
          
        }
        
    }

脚本

<script>


          $(document).ready(function(){
    $(".allTable").on("click", function(){
        var view_id = $(this).data('id')
    
        $.ajax({
         
           url: "<?=site_url('network/view')?>",

          type: "post",
          dataType: "json",
          data: {
            view_id: view_id  
          },
            success: function(data){
              
              var tbody ="";
              var item =data.post; 


            for(var key in item) {
                    tbody +="<tr>";
                    tbody += "<td>"+item[key].userID+"</td>";
                    tbody += "<td>"+item[key].uuid+"</td>";
                    tbody += "<td>"+item[key].firstname+" "+item[key].lastname+"</td>";
                    tbody += "<td>"+item[key].email+"</td>";
                    tbody += "<td>"+item[key].mobile+"</td>";
                    tbody += "<td>"+item[key].username+"</td>";
                    tbody +="</tr>"
                                 }
           $("span[data-id="+view_id+"]").text(Object.keys(item).length); 
          $(".tbody").html(tbody);
          $('#editModal').modal('show');
            }
     
        });
        })
        });
 
</script>

【问题讨论】:

  • 没有。它仅在我单击带有表格的按钮模式后显示值。我的目标是,当我访问该页面时,它会自动显示徽章中的用户数,而无需单击按钮模式。正如我上面所说的我的形象。谢谢
  • 你的问题解决了吗??

标签: html ajax codeigniter


【解决方案1】:

查看代码:

使用num_rows(),您首先执行查询,然后您可以检查获得了多少行。

  <?php 
   foreach($result as $rows) { 

<button data-id="<?php echo $rows->userID; ?>" class=" allTable btn btn-danger btn-sm text-bold" type="button"  data-toggle="modal" data-target="#editModal">
<i class="fas fa-users"></i>
<span data-id="" class="badge badge-warning navbar-badge">

<?php echo $data=$this->db->get_where('table_name',array('UUID'=>$rows->userID))->num_rows(); ?>

</span>
</button> 

}
?>

【讨论】:

  • 你好,它会产生错误。请查看我提供的链接:prnt.sc/w9n2vs
  • mx 加载程序错误:第 363 行查看错误:第 154 行
  • 为什么要在MX/Loader.php写这段代码??
  • 不,我是在视图中写的。请看这个:prnt.sc/w9nakf 你发送的代码是我在我的 VIEW 代码上替换的。更换后,它会触发2个错误。 1 个来自 MX,1 个来自视图。
猜你喜欢
  • 2021-04-03
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多