Search code examples
mysqlajaxcodeigniter

Fetching data based on eventID


newbie here. My target is to show a data that based on eventID. For example I registered a data in eventID="1". When I click my view button, the modal will show with all the entries I registered under eventID="1" and so on. I made a query but I'm getting an error. How can I make this work? I provided codes and screenshot of my target. Thank you in advance and have a good day.

Screenshot with explanations When I click view button, this is what happens

View:

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

Ajax:

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
               },
    
           ],
    
       });
       
    
    
       
        }

Controller:

//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);
    }

Model:

    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();
    }
    

Solution

  • What sorting did you use to display that table?

    Example if you display the results in table by query order by Id and Desc order, you should foreach the result and on the button to display the table use href wich will trigger the modal and number the modal id with the id from table.

    At the end of the page, call a query the same way as you query the table, open a foreach function before the modal and name the modal with the id from the database (the way you name then in the href at the button).

    By that you should’ve get what you wanted.

    Please comment if you’re still confused.