[eluser]qpixo[/eluser]
Just started learning CI,
Does anyone know how to make the pagination search with keyword running on second page link?
I would like to add a pagination to my search
My keyword search seems to work fine, I'm using one search form but the problem is I can't make second page link working. Assuming there are two different Requests made (when user click on search button, pagination links)
I set keyword to session then add it in an array. How can I make running URL?
Controller:
Code:
// Constructor
...
$this->load->library('pagination');
$this->load->library('table');
$this->load->model('film_model');
public function index() {
// If search button doesn't press
if(!isset($_POST['search'])) {
// reset the search terms session
$this->session->unset_userdata($this->tabKeyword);
// application_search is our view that contains the form
$this->displayFilm();
} else {
$keyword = $this->input->post('keyword');
if ($keyword) {
// Add the input keyword in tabKeyword
$this->tabKeyword['search_keyword'] = $this->input->post('keyword');
// Set keyword value into session
$this->session->set_userdata($this->tabKeyword);
$this->displaySearchResult();
}
}
}
public function displaySearchResult() {
$data['title'] = "Search Results";
// Get the third parameter defined of segment in URL
$offset = $this->uri->segment(3);
// Get total results of keyword search item
$total_results = $this->film_model->totalSearchItem($this->input->post('keyword'));
$config['base_url'] = base_url() . '/index.php/film_controller/displaySearchResult';
$config['total_rows'] = $total_results; // Must return the number of keyword search result
$config['uri_segment'] = '3';
$config['per_page'] = $this->results_per_page;
// Init pagination, add results search items in searchResults array then load it in View
$this->pagination->initialize($config);
$data['searchResults'] = $this->film_model->getSearch($this->input->post('keyword'), $this->results_per_page, $offset);
$this->load->view('film_result', $data);
}
public function displayFilm() {
$total = $this->film_model->countAll();
$offset = $this->uri->segment(3);
$config['base_url'] = base_url() . '/index.php/film_controller/displayFilm';
$config['total_rows'] = $total;
$config['uri_segment'] = '3';
$config['per_page'] = $this->results_per_page;
$this->pagination->initialize($config);
$data['results'] = $this->film_model->getData($config['per_page'], $offset);
$this->load->view('film', $data);
}
Model:
Code:
function countAll() {
return $this->db->count_all('film_list');
}
public function getData($limit, $offset) {
$this->db->select('actors, title, description, category');
$query = $this->db->get('film_list', $limit, $offset);
return $query;
}
public function getSearch($keyword, $limit, $offset) {
if (!empty($keyword)) {
$this->db->like('actors', $keyword);
$this->db->or_like('title', $keyword);
$this->db->or_like('category', $keyword);
$this->db->order_by('title', 'ASC');
$results = $this->db->get('film_list', $limit, $offset);
return $results->result_array();
}
}
public function totalSearchItem($keyword) {
if (!empty($keyword)) {
$this->db->select('COUNT(*) as rows');
$this->db->like('actors', $keyword);
$this->db->or_like('title', $keyword);
$this->db->or_like('category', $keyword);
$query = $this->db->get('film_list')->row();
return $query->rows;
}
}