SEARCH AND PAGINATION NOT WORKING WITH THIS CODE.

SEARCH AND PAGINATION NOT WORKING WITH THIS CODE.

saadchaudharysaadchaudhary Posts: 1Questions: 1Answers: 0
edited April 2020 in Free community support

WHEN PAGE FRESH OVERALL DATA IS SHOWING CORRECT BUT FILTER AND PAGINATON NOT FILTERING THE DATA AND DRAW RETURNING 0 EVERYTIME. ANYONE HELP PLEASE

SCRIPT

<!-- Datatable JS -->
<script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<!-- /datepicker -->
<!-- Script -->
<script type="text/javascript">
    $(document).ready(function(){
        $('#search-logs').DataTable({
            'processing': true,
            'serverSide': true,
            'paging': true,
            'scrollY': false,
            'aLengthMenu': [[25, 50, 75, -1], [25, 50, 75, "All"]],
            'iDisplayLength': 25,
            'serverMethod': 'post',
            'ajax': 
            {
               'url':'<?=base_url()?>admin/search/JSON',
                "deferLoading": 57
           },
           'columns': [
           { data: 'count' },
           { data: 'property_purpose' },
           { data: 'country' },
           { data: 'location' },
           { data: 'property_type' },
           { data: 'price_range' },
           { data: 'area_range' },
           { data: 'bedrooms_range' },
           { data: 'amenities_range' },
           { data: 'keyword_search' },
           { data: 'search_on' },
           ]
       });
    });
</script>

MODEL FUNCTION

function getLogsList($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 = " (property_purpose like '%".$searchValue."%' or country like '%".$searchValue."%' or location like'%".$searchValue."%' or property_type like'%".$searchValue."%' or price_range like'%".$searchValue."%' or area_range like'%".$searchValue."%' or bedrooms_range like'%".$searchValue."%' or amenities_range like'%".$searchValue."%' or keyword_search like'%".$searchValue."%' or search_on like'%".$searchValue."%') ";
}

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

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

## Fetch records
$this->db->select('*');
if($searchQuery != '')
$this->db->where($searchQuery);
$this->db->order_by($columnName, $columnSortOrder);
$this->db->limit($rowperpage, $start);
$records = $this->db->get('search_logs')->result();

$data = array();

$count = 1;
foreach($records as $record ){

$data[] = array( 
   "count"=>$count,
   "property_purpose" =>($record->property_purpose == '' ? 'not given' : $record->property_purpose),
   "country"          =>($record->country          == '' ? 'not given' : $record->country),
   "location"         =>($record->location         == '' ? 'not given' : $record->location),
   "property_type"    =>($record->property_type    == '' ? 'not given' : $record->property_type),
   "price_range"      =>($record->price_range      == '' ? 'not given' : $record->price_range),
   "area_range"       =>($record->area_range       == '' ? 'not given' : $record->area_range),
   "bedrooms_range"   =>($record->bedrooms_range   == '' ? 'not given' : $record->bedrooms_range),
   "amenities_range"  =>($record->amenities_range  == '' ? 'not given' : $record->amenities_range),
   "keyword_search"   =>($record->keyword_search   == '' ? 'not given' : $record->keyword_search),
   "search_on"        =>($record->search_on        == '' ? 'not given' : $record->search_on)
);
$count++;
}

## Response
$response = array(
"draw" => intval($draw),
"iTotalRecords" => $totalRecords,
"iTotalDisplayRecords" => $totalRecordwithFilter,
"aaData" => $data
);

return $response; 
}

CONTROLLER FUNCTION

public function JSON()
{
    // POST data
    $postData = $this->input->post();

    // Get data
    $data = $this->Searched_model->getLogsList();
    echo json_encode($data);
}

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

This discussion has been closed.