Welcome Guest, Not a member yet? Register   Sign In
Search results pagination displays ALL the posts
#1

I am working on a basic blog application with Codeigniter 3.1.8. and Bootstrap 4. The application has a search posts functionality.
The form in the header view:

Code:
<form method="get" action="<?php echo base_url('posts/search') ?>" id="search_form" class="w-100 py-1 px-2 px-md-3 px-lg-5" accept-charset="utf-8">
   <div class="input-group <?php if(form_error('search')) echo 'has-error';?>">
     <input class="form-control form-control-dark" type="text" name="search" placeholder="Search posts..." aria-label="Search">
     <?php if(form_error('search')) echo form_error('search'); ?>
     <div class="input-group-append">
       <button class="btn btn-success" type="submit"><i class="fa fa-search"></i></button>
     </div>
   </div>
</form>

Since the posts are paginated, so should the search results. The index()  and search()  methods are both part of the same Posts controller, so I have tried to NOT repeat the pagination's code:


Code:
private function _initPagination($path, $totalRows, $query_string_segment = 'page') {
   //load and configure pagination
   $this->load->library('pagination');
   $config['base_url'] = base_url($path);
   $config['query_string_segment'] = $query_string_segment;
   $config['total_rows'] = $totalRows;
   $config['per_page'] = 12;
   if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
     $_GET[$config['query_string_segment']] = 1;
   }
   $this->pagination->initialize($config);

   $limit = $config['per_page'];
   $offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;

   return ['limit' => $limit, 'offset' => $offset];
 }

 public function index() {
   //call initialization method
   $config = $this->_initPagination("/posts", $this->Posts_model->get_num_rows());

   $data = $this->Static_model->get_static_data();
   $data['categories'] = $this->Categories_model->get_categories();

   //use limit and offset returned by _initPaginator method
   $data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
   $this->load->view('partials/header', $data);
   $this->load->view('posts');
   $this->load->view('partials/footer');
 }

 public function search() {
  // Force validation since the form's method is GET
  $this->form_validation->set_data($this->input->get());
  $this->form_validation->set_rules('search', 'Search term', 'required|trim|min_length[3]');
  $this->form_validation->set_error_delimiters('<p class = "error search-error"> ', ' </p>
      ');
   // If search fails
  if ($this->form_validation->run() === FALSE) {
      return $this->index();
  } else {
      $expression = $this->input->get('search');
      $posts_count = $this->Posts_model->search_count($expression);
      $query_string_segment = 'search=' . $expression . '&page';
      $config = $this->_initPagination("/posts/search", $posts_count, $query_string_segment);
      $data = $this->Static_model->get_static_data();
      $data['categories'] = $this->Categories_model->get_categories();
      //use limit and offset returned by _initPaginator method
      $data['posts'] = $this->Posts_model->search($expression, $config['limit'], $config['offset']);
      $data['expression'] = $expression;
      $data['posts_count'] = $posts_count;
      $this->load->view('partials/header', $data);
      $this->load->view('search');
      $this->load->view('partials/footer');
  }
}

The model:

Code:
public function search_count($expression) {
   $query = $this->db->like('title', $expression)
                     ->or_like('description', $expression)
                     ->or_like('content', $expression);
   $query = $this->db->get('posts');
   return $query->num_rows();  
}

public function search($expression, $limit, $offset) {
   $query = $this->db->like('title', $expression)
                       ->or_like('description', $expression)
                       ->or_like('content', $expression);
   $this->db->order_by('posts.id', 'DESC');
   $query = $this->db->get('posts', $limit, $offset);
   return $query->result();
}

The PROBLEM:


The first page of search results displays 12 items containing the search expression, as expected, but all the other pages, display all the posts again.


Since 

Code:
$config['query_string_segment']

 is used by the pagination, the line 

Code:
$query_string_segment = 'search=' . $expression . '&page';

 should help pass the search expression, via $_GET[]
 to the pages 1, 2 and so on. But the page items link does not  keep "=" and "&":

Code:
<a href="http://localhost/ciblog/posts/search?search%3Dharum%26page=2" data-ci-pagination-page="2">2</a>

Why does that happen? Where is my mistake?
Reply
#2

Have you thought about using js datatables?
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#3

set TRUE for
$config['reuse_query_string']
Reply




Theme © iAndrew 2016 - Forum software by © MyBB