I have a controller function that displays all records, I am using the pagination class to divide the records in pages. The controller looks a bit like that
class Example extends CI_Controller {
[...]
function All()
{
//Gets all the records
//Pagination uri_segment set to 3
}
}
Now i added the ability to search the records. I need the search term to be in the uri so the function is like function All($search_term = false)
. My issue is that the function may or may not have search terms. and depending on that, the uri segment of the pagination will change. without a search term uri_segment is 3, with it is 4.
Is there a way to fix that while using the same function and having the search term in the url?
It can be done various ways, personally I would do something simple like store it in session or cookie, but you can do URI as well refer to the CI forum for this one:
http://codeigniter.com/forums/viewthread/59364/
Keep in mind that people may type non URI friendly strings into your search term and you will have to urlencode / decode the values in order to put them in a string, something you don't need with cookies, or sessions. But again I see the benefit for copy & pasting the link to someone for search results.
You can use get parameter for the query string. For example:
search.php
function search() {
$q = $this->input->get('q');
$page = $this->uri->segment(3);
$results = $this->my_model->search($q, $page);
$this->load->view('search_tpl', $results);
}
search_tpl.php
echo form_open(current_url(), array('method' => 'get'));
echo form_input('q');
echo form_submit('search', 'Search');
echo form_close();
You can use page_query_string in pagination $config variable
$config['page_query_string'] = TRUE;
After enabling this option, $_GET will be used to create pagination link.
Example result: example/all/?per_page=20
You simply change
$config["uri_segment"] = 3;
to $config["uri_segment"] = 4;
and also change base_url like
$config["base_url"] = "site_url('student/index')".$search_key;
and get the search_key by
$key = $this->uri->segment(3);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.