Welcome Guest, Not a member yet? Register   Sign In
How can I use the pagination from the index() method inside the search() method?
#1

(This post was last modified: 09-20-2018, 10:52 PM by Ajax30.)

I am working on a basic blog application in Codeigniter 3.1.8. The posts are paginated. The application uses a pagination.php configuration file.

There is a search box for searching posts. I wish to paginate the search results and, since the methods index() and search() are in the same Posts controller, I am looking for a way avoid code redundancy by using the pagination inside for the search results too.

In the posts controller I have:
Code:
class Posts extends CI_Controller {
 public function __construct() {
   parent::__construct();
   $this->load->model('Static_model');
   $this->load->model('Posts_model');
   $this->load->model('Categories_model');
   $this->load->model('Comments_model');
 }

 public function index() {
   //load and configure pagination
   $this->load->library('pagination');
   $config['base_url'] = base_url("/posts");
   $config['query_string_segment'] = 'page';
   $config['total_rows'] = $this->Posts_model->get_num_rows();
   $config['per_page'] = 12;
   if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
     $_GET[$config['query_string_segment']] = 1;
   }
   $limit = $config['per_page'];
   $offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
   $this->pagination->initialize($config);
   $data = $this->Static_model->get_static_data();
   $data['categories'] = $this->Categories_model->get_categories();
   $data['posts'] = $this->Posts_model->get_posts($limit, $offset);
   $this->load->view('partials/header', $data);
   $this->load->view('posts');
   $this->load->view('partials/footer');
 }

 public function search() {
   $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->post('search');
       $data = $this->Static_model->get_static_data();
       $data['categories'] = $this->Categories_model->get_categories();
       $data['posts'] = $this->Posts_model->search($expression, $limit, $offset);
       $data['expression'] = $expression;
       $this->load->view('partials/header', $data);
       $this->load->view('search');
       $this->load->view('partials/footer');
     }
   }
 }

The Posts_model model has the code for both the posts list and the search results:

Code:
public function get_posts($limit, $offset) {
   $this->db->order_by('id', 'DESC');
   $query = $this->db->get('posts', $limit, $offset);
   return $query->result();
}

public function search_count() {
    $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');
   return $query->result();
}

The code above is nonfunctional. The browser throws this error:

Code:
Message: Undefined variable: limit
Filename: controllers/Posts.php

I got stuck in my attempt to borrow the pagination from index() 
to search().

What is missing?
Reply


Messages In This Thread
How can I use the pagination from the index() method inside the search() method? - by Ajax30 - 09-17-2018, 12:52 PM



Theme © iAndrew 2016 - Forum software by © MyBB