我在数据表中添加了分页,但是表记录在重复

时间:2019-07-23 12:45:08

标签: php mysql sql codeigniter datatable

在我的分页代码中,从表中获取数据表明其数据搜索和分页有效,但记录却多次显示

enter image description here

在上图中,您可以看到记录的修复。

控制器:

       public function getData(){ 
           $postData = $this->input->post();
           $data = $this->dispatchModel->viewData($postData);
           echo json_encode($data);
  }

型号:

function viewData($postData=null){
     $response = array();
     ## Read value
     $draw            = $postData['draw'];
     $start           = $postData['start'];
     $rowperpage      = $postData['length']; // Rows display per page
     $columnIndex     = $postData['order'][0]['column']; // Column index
     $columnName      = $postData['columns'][$columnIndex]['data']; // Column name
     $columnSortOrder = $postData['order'][0]['dir']; // asc or desc
     $searchValue     = $postData['search']['value']; // Search value

     ## Search 
     $searchQuery = "";
     if($searchValue != ''){

        $searchQuery = " (b.disp_ch_driver like'%".$searchValue."%') ";
     }

     ## Total number of records without filtering
     $this->db->select('count(*) as allcount');
     $records = $this->db->get('dispatch_challan')->result();
     $totalRecords = $records[0]->allcount;

     ## Total number of record with filtering
     $this->db->select('count(*) as allcount');
     if($searchValue != '')
        $this->db->where($searchQuery);
     $records = $this->db->get('dispatch_challan b')->result();
     $totalRecordwithFilter = $records[0]->allcount;

     ## Fetch records
     $this->db->select('vn.vehicle_no as vehicle,bn.branch_name as from_branch_name,bn1.branch_name as to_branch_name,b.*,vn.*,bn.*,bn1.*');
             $this->db->from('dispatch_challan b');
             $this->db->join('vehicle vn', 'b.disp_ch_lorryno=vn.vehicle_id','left');
             $this->db->join('branch  bn', 'b.disp_ch_from=bn.branch_id','left');
             $this->db->join('branch  bn1','b.disp_ch_to=bn1.branch_id','left');

             $this->db->where('b.status','active');

     if($searchValue != '')
     $this->db->where($searchQuery);
     $this->db->order_by($columnName, $columnSortOrder);
     $this->db->limit($rowperpage, $start);
     $records = $this->db->get('dispatch_challan')->result();

     $data = array();
     foreach($records as $record ){
        $data[] = array( 
           "disp_ch_no"          =>$record->disp_ch_no,
           "disp_ch_date"        =>$record->disp_ch_date,
           "from_branch_name"    =>$record->from_branch_name,
           "to_branch_name"      =>$record->to_branch_name,
           "vehicle"             =>$record->vehicle,
           "disp_ch_pan"         =>$record->disp_ch_pan,
           "disp_ch_driver"      =>$record->disp_ch_driver,
           "disp_ch_owner"       =>$record->disp_ch_owner,
           "disp_ch_remark"      =>$record->disp_ch_remark
        ); 
     }
     ## Response
     $response = array(
        "draw" => intval($draw),
        "iTotalRecords" => $totalRecordwithFilter,
        "iTotalDisplayRecords" => $totalRecords,
        "aaData" => $data
     );
     return $response; 
   }

在上述模型中,有分页代码,但记录多次显示。 在我的数据库中,我只有5条记录。

1 个答案:

答案 0 :(得分:0)

由于连接,

问题可能不是表分页中的数据库查询。尝试使用邮递员运行模型或api,并观察重复或不重复的结果。