I am trying to add search in my lists. I am using codeigniter and pagination for listing. The search is just the database search as a sample id . The search works perfect in the first page of pagination. But when it comes to other pages it does not work.
This is the code for my controller.
$this->clear_sample_id_catche();
$out=$this->Mgeneral->listData();
$per_page = 10;
$total = $this->Mgeneral->countData();
$data = $this->Mgeneral->listData($per_page, $this->uri->segment(3));
$base_url = site_url('general/listData');
$config['base_url'] = $base_url;
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = '3';
$this->pagination->initialize($config);
$data=array('body'=>'list','out'=>$data);
$this->load->view('template',$data);
I added form to search in my view.
Can any one suggest my what the problem is??
Thanks in advance.
You must be forgetting to pass the search query for the pagination. Seeing how the page number is the third segment of the query string, I'm guessing that the search form will do a POST
action when it gets submitted by the user. After the user submitted the form, of course it will work. Because they just did a POST
request, and you fetch the search query from $_POST
by using $this->input->post('user_input')
or something similar. But then, when they try to navigate to another page by clicking on the page number link (which is a GET
request), the search fails because the superglobal $_POST
array is empty.
A solution to solve this problem is to set a session value (I recommend that you use CodeIgniter's Session Class) based on the user input and instead of using the search query from $_POST
, and use the one stored in session instead.
Another way you can do it, is to make another page for the search result (assuming it is currently in the same page as the form, i.e. in the same controller function). Then make the search form do a GET
request to its own controller function which will then encode the search query in base64
, and replace the =
(equal sign) from the encoded string with a .
. After that, redirect the user to the search result page while passing the encoded search query as the third segment in the URL query string (the page number will be the fourth). And in that function replace the .
back to =
and use base64_decode()
to get the search query and pass it to the function that will generate the search result.
Of course, there are lots of other ways to achieve the same result. Just google it.