John Davidson

php - Pagination fetching rows more than once in search records in CodeIgniter

0 comments
Message:


I am working on this from quite while, and yet my pagination is although working so far, but looking at it closer it seems that on the last page it's pulling in previous results to fill the page in.


So say I want ten per page and have fourteen results. The first page has ten results, and so does the second. When it should be the first has ten and the second has four. It would be fine if it was just repeating one result, but it's irritating to have to scroll through six previous results. Any help would be much appreciated thanks in advance.


Search result url



https://xxxx.xxx/site/search?id=2&query=2+Pc+Meal&page=2



id=2 is the restaurant ID & query param is for entered search keyword


controller


 // THIS FUNCTION IS RESPONSIBLE FOR SHOWING SEARCH RESULTS
function search()
{

$page_data['restaurant_id'] = sanitize($_GET['id']);
$page_data['query'] = sanitize($_GET['query']);
$page_data['page_name'] = 'search/index';
$page_data['page_title'] = 'Search result';

/**PAGINATION STARTS**/
$total_rows = $this->menu_model->count_search_menu($page_data['query'], $page_data['restaurant_id']);
$page_size = 10;
$pagination_url = site_url('site/search');
$config = pagintaion($total_rows, $page_size, $pagination_url);
$current_page = sanitize($this->input->get('page', 0));
$this->pagination->initialize($config);

$page_data['total_rows'] = $total_rows;
$page_data['search_result'] = $this->menu_model->get_searched_menu_by_name_and_restaurant($page_data['query'], $page_data['restaurant_id'], $page_size ,$current_page);

$this->load->view(frontend('index'), $page_data);

}

MODAL


 // COUNT TOTAL ROWS
public function count_search_menu($query, $restaurant)
{
$this->db->where('restaurant_id', $restaurant);
$this->db->like('name', $query, 'both'); //both = %query%, before = %query, after = query%
$menu = $this->db->get($this->table);
$result = $menu->result();
$count = count($result);
return $count;
}

// GET MENU BY SEARCH NAME AND RESTAURANT ID
public function get_searched_menu_by_name_and_restaurant($query, $restaurant, $limit, $start)
{
$this->db->where('restaurant_id', $restaurant);
$this->db->like('name', $query, 'both'); //both = %query%, before = %query, after = query%

// Executes: SELECT * FROM mytable LIMIT 10 OFFSET 20
// get([$table = ''[, $limit = NULL[, $offset = NULL]]])
// $this->db->get('mytable', 10, 20);

$menu = $this->db->get($this->table, $limit, $start);
return $menu->result();
}

Visual look example:


per page = 10


total rows = 25



As u can see above, all 3 pages show 10 items, instead it should be show 10 on 1st page, 10 on 2nd and 5 on 3rd page.



My Answer:
To fix this issue, you need to modify your query to only fetch the required number of rows for each page. Here's an example of how you can achieve this in CodeIgniter:

1. Modify your controller method that handles the search results to include pagination:

php
public function search() {
$id = $this->input->get('id');

$this->load->library('pagination');

$config = array();
$config["base_url"] = base_url() . "site/search?id=$id";
$config["total_rows"] = $this->your_model->get_search_results_count($id);
$config["per_page"] = 10;
$config["uri_segment"] = 3;

$this->pagination->initialize($config);

$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

$data["results"] = $this->your_model->get_search_results($id, $config["per_page"], $page);

$this->load->view('search_results', $data);
}


2. Modify your model to fetch only the required number of rows for each page:

php
public function get_search_results($id, $limit, $offset) {
$this->db->limit($limit, $offset);
$query = $this->db->get_where('your_table', array('id' => $id));

return $query->result();
}


By using the `limit` method in your model, you can ensure that only the required number of rows are fetched for each page of the search results. This should prevent the issue of repeating previous results on the last page of the pagination.

Rate this post

3 of 5 based on 9545 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us