【问题标题】:Fetching data based on eventID根据 eventID 获取数据
【发布时间】:2021-07-23 11:40:51
【问题描述】:

这里是新手。我的目标是显示基于 eventID 的数据。例如,我在 eventID="1" 中注册了一个数据。当我单击查看按钮时,模式将显示我在 eventID="1" 下注册的所有条目,依此类推。我进行了查询,但出现错误。我怎样才能使这项工作?我提供了目标的代码和屏幕截图。提前感谢您,祝您有美好的一天。

查看:

 <div class="modal fade" id="viewModal">
        <div class="modal-dialog modal-xl">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title"></h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
              <table id="entryTable" class="table table-bordered table-hover" cellspacing="0">
                
        
                 <thead>         
                      <tr>
                        <th>Entry ID</th>
                        <th>Entry Name</th>  
                        <th>Owner</th>
                        <th># Of Fights</th>
                    </tr>
                </thead>
            
            <tbody>
            
            </tbody>
 
            
             </table>
            </div>
            <div class="modal-footer justify-content-between">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

            </div>
         </div>
        </div>
 </div>

阿贾克斯:

function view_person(eventID)
   {
   

   
   
   
    $('.form-group').removeClass('has-error'); // clear error class
    $('.help-block').empty(); // clear error string
    $('#viewModal').modal('show'); // show bootstrap modal
    $('.modal-title').text('View Entries for this Event'); // Set Title to Bootstrap modal title
    
    
      table = $('#entryTable').DataTable({ 
            dom: 'lBfrtip',
            buttons: [
               'print', 'csv', 'copy', 'excel', 'pdf'
           ],
           "processing": true, //Feature control the processing indicator.
           "serverSide": true, //Feature control DataTables' server-side processing mode.
           "order": [], //Initial no order.
           
    
           // Load data for the table's content from an Ajax source
           "ajax": {
             url : "<?php echo site_url('transaction/entryFetch')?>/" + eventID,
               "type": "POST"
           },
    
           //Set column definition initialisation properties.
           "columnDefs": [
               { 
                   "targets": [ 0 ], //first column
                   "orderable": false, //set not orderable
               },
               { 
                   "targets": [ -1 ], //last column
                   "orderable": false, //set not orderable
               },
    
           ],
    
       });
       
    
    
       
        }

控制器:

//this is my fetching inside of viewModal
    function entryFetch()
        {
            
            $list = $this->entryy->get_datatables();
            
            $data = array();
            $no = $_POST['start'];
            foreach ($list as $person) {
                $no++;
                $row = array();
                $row[] = $person->entryID;
                $row[] = $person->entryName;
                $row[] = $person->owner;
                $row[] = $person->noOfFight;
               
    
                
                
                $data[] = $row; 
            }
                
                $output = array(
                    "draw" => $_POST['draw'],
                    "recordsTotal" => $this->entryy->count_all(),
                    "recordsFiltered" => $this->entryy->count_filtered(),
                    "data" => $data,
                );
                //output to json format
                echo json_encode($output);
        }



 //this is where I specifies my button's value eventID.
    
        
      function ajax_list()
    {
        
        
        $list = $this->transactions->get_datatables();
        $data = array();
        $no = $_POST['start'];
        foreach ($list as $person) {
            $no++;
            $row = array();
            $row[] = $person->eventID;
            $row[] = $person->description;
            $row[] = $person->derbyDate;
            $row[] = $person->fightType;
            $row[] = $person->host;
            $row[] = $person->venue;            
            $row[] = $person->createdBy;
            $row[] = $person->dateCreated;
            $row[] = $person->updatedBy;
            $row[] = $person->dateUpdated;
            
     
            
           
            $row[] = '
                  <a class="btn btn-sm btn-success" href="javascript:void(0)" title="View" onclick="view_person('."'".$person->eventID."'".')"><i class="glyphicon glyphicon-pencil"></i> View';
            
            $data[] = $row;
        }
        
        $output = array(
            "draw" => $_POST['draw'],
            "recordsTotal" => $this->transactions->count_all(),
            "recordsFiltered" => $this->transactions->count_filtered(),
            "data" => $data,
        );
        //output to json format
        echo json_encode($output);
    }

型号:

    var $table = 'entries';
    var $column_order = array(null,'id','entryName','owner','noOfFights');
    var $order = array('id' => 'asc');
    var $column_search = array('id','entryName','owner','noOfFights');
 
    
    private function _get_datatables_query()
    {
        
        $this->db->from($this->table);
      
        $this->db->where('eventID',$_POST['eventid']);
     
        
        $i = 0;
        
        foreach ($this->column_search as $item) // loop column
        {
            if($_POST['search']['value']) // if datatable send POST for search
            {
                
                if($i===0) // first loop
                {
                    $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
                    $this->db->like($item, $_POST['search']['value']);
                }
                else
                {
                    $this->db->or_like($item, $_POST['search']['value']);
                }
                
                if(count($this->column_search) - 1 == $i) //last loop
                    $this->db->group_end(); //close bracket
            }
            $i++;
        }
        
        if(isset($_POST['order'])) // here order processing
        {
            $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        }
        else if(isset($this->order))
        {
            $order = $this->order;
            $this->db->order_by(key($order), $order[key($order)]);
        }
    }
    
    
    //fetch view
    function get_datatables()
    {
        $this->_get_datatables_query();
        
        if($_POST['length'] != -1)
            $this->db->limit($_POST['length'], $_POST['start']);
            $query = $this->db->get();
            return $query->row();
            
            
           
    }
    
    function count_filtered()
    {
        $this->_get_datatables_query();
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    public function count_all()
    {
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }
    

【问题讨论】:

  • 你得到了什么错误,哪一行触发了错误?
  • 没有行错误。只是一个无效的 json 响应角色。具体来说:DataTables 警告:table id=entryTable - JSON 响应无效。有关此错误的更多信息,请参阅datatables.net/tn/1
  • 那么请先检查json响应,看看有什么问题。基于此,您可以追溯错误的来源。我们不能那样做。
  • 如果我获取整个表格,则没有错误。但是当我在做一个 select where eventID.事情不正常。
  • 那你需要确定过滤表的时候出了什么问题。很抱歉,除非您能帮我们缩小问题范围,否则我们无法为您提供帮助!

标签: mysql ajax codeigniter


【解决方案1】:

您使用什么排序来显示该表格?

例如,如果您按 Id 和 Desc 顺序的查询顺序在表中显示结果,您应该 foreach 结果并在显示表的按钮上使用 href 将触发模态并使用表中的 id 为模态 ID 编号.

在页面的末尾,以与查询表相同的方式调用查询,在模态之前打开一个 foreach 函数并使用数据库中的 id 命名模态(然后在 href 中的命名方式按钮)。

这样你就应该得到你想要的。

如果您仍然感到困惑,请发表评论。

【讨论】:

  • 您好,感谢您的帮助。我找到了另一种方法。我只是用另一种方法来解决我的问题。不过,我会给你一个赞成票和一个正确的答案。谢谢你。 ^_^
猜你喜欢
  • 2013-01-21
  • 2017-12-19
  • 2014-08-27
  • 2019-06-23
  • 2021-11-27
  • 1970-01-01
  • 2015-11-09
  • 2020-03-25
  • 1970-01-01
相关资源
最近更新 更多