DataTable using PHP

前端 未结 2 1433
说谎
说谎 2021-01-16 05:09

Is it possible to code a DataTable using PHP?

When searching online all tutorials etc are using ajax, which I\'m not to comfortable with so just wondering if there

2条回答
  •  孤街浪徒
    2021-01-16 05:36

    1)Include Css and JS files:

    
    
    
    
    

    2)Create function in controller file:

    function MyData()
            {
                $aColumns = array('user_id','username','address');
    
                $sTable = 'user';
    
                $iDisplayStart = $this->input->get_post('iDisplayStart', true);
                $iDisplayLength = $this->input->get_post('iDisplayLength', true);
                $iSortCol_0 = $this->input->get_post('iSortCol_0', true);
                $iSortingCols = $this->input->get_post('iSortingCols', true);
                $sSearch = $this->input->get_post('sSearch', true);
                $sEcho = $this->input->get_post('sEcho', true);
    
                // Paging
                if(isset($iDisplayStart) && $iDisplayLength != '-1')
                {
                    $this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
                }
    
                // Ordering
                if(isset($iSortCol_0))
                {
                    for($i=0; $iinput->get_post('iSortCol_'.$i, true);
                        $bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
                        $sSortDir = $this->input->get_post('sSortDir_'.$i, true);
    
                        if($bSortable == 'true')
                        {
                            $this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
                        }
                    }
                }
    
                /* 
                 * Filtering
                 * NOTE this does not match the built-in DataTables filtering which does it
                 * word by word on any field. It's possible to do here, but concerned about efficiency
                 * on very large tables, and MySQL's regex functionality is very limited
                 */
                if(isset($sSearch) && !empty($sSearch))
                {
                    for($i=0; $iinput->get_post('bSearchable_'.$i, true);
    
                        // Individual column filtering
                        if(isset($bSearchable) && $bSearchable == 'true')
                        {
                            $this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
                        }
                    }
                }
    
                // Select Data
    
    
                $this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);               
                $rResult = $this->db->get($sTable);
    
                // Data set length after filtering
                $this->db->select('FOUND_ROWS() AS found_rows');
                $iFilteredTotal = $this->db->get()->row()->found_rows;
    
                // Total data set length
                $iTotal = $this->db->count_all($sTable);
    
                // Output
                $output = array(
                    'sEcho' => intval($sEcho),
                    'iTotalRecords' => $iTotal,
                    'iTotalDisplayRecords' => $iFilteredTotal,
                    'aaData' => array()
                );
    
                foreach($rResult->result_array() as $aRow)
                {
                    $row = array();
    
                    foreach($aColumns as $col)
                    {
                         $row[] = $aRow['user_id'];
                         $row[] = $aRow['username'];
                         $row[] = $aRow['address'];
                    }
    
                    $output['aaData'][] = $row;
                }
    
                echo json_encode($output);
    
            }
    

    3)Create a table in view file:

    User Id Username Address Edit / Delete

    4)Write AJAX:

    
    

提交回复
热议问题