Im trying to implement pagination using multiple searching criteria.
Supposed I Have student table. I also use pagination 开发者_JAVA技巧when the list of student displayed.
The pagination link is. site_url . '/student/page/';
so I use $config['uri_segment'] = 1
;
so the pagination link will be
<a href="http://mysite/index.php/student/page/0">1</a>
<a href="http://mysite/index.php/student/page/1">2</a>
and son.
After that I wanna search student data using 3 searching criteria implemented using textfield.
id name address.
user can search by id or name or address or combination of the three criteria. the url become
http://mysite/index.php/student/page/0
href=http://mysite/index.php/student/page/1
and son.
but I use get method for searching. and while trying to search using the search criteria field the url become
href="http://mysite/index.php/student/page/1?id=1&name=a&address=b
the problem occurred when I try create pagination based on criteria. because the pagination link have contain query string i don't know how to create become
href="http://mysite/index.php/student/page/0?id=1&name=a&address=b
href="http://mysite/index.php/student/page/1?id=1&name=a&address=b
or do you have a best practice to solve this problem ?
To solve that problem, I try using $this->uri->uri_to_assoc()
. First I create array asocc for pagination link.
$array = array('id' => '001', 'name' => 'a', 'address' => 'canada');
the url become
id/001/name/a/address/canada
. I use $this->uri->uri_to_assoc()
function to get key and value of the segment.
array (
id => 001,
name=>a,
address=>canada
)
but while there some searching criteria that not included while searching. let say, the user only search by name and address. the array become
$array = array('id' => '', 'name' => 'a', 'address' => 'canada');
and the url id/name/a/address/canada
the assoc array become
array (
id => name,
a=>address,
canada=>
)
the assoc array is not disorganized again. so I can't get the right value of the assoc array.
I think i will set the identifier to the searching criteria if not included. supposed i put #
.
if isset($_GET['id']) then
$id = '#'
else
$id = $_GET['id']
$array = array('id' => $id, 'name' => 'a', 'address' => 'canada');
How about that ... ? or if there are another best practice ?
Thanks
I've always found it somewhat a pain to deal with uri's in ci.
Is there a way you can set a default of some kind for your values if the user doesn't include that as part of their search? or even not include the key? so it would return something like
id/10/name/false/address/canada
or
id/10/address/canada
then you can
$uri = $this->uri->uri_to_assoc();
$id = array_key_exists("id", $uri) ? $uri['id'] : false;
$id = $id == 'false' ? false : $id;
$query .= $id ? "AND id = $id" : "";
etc...
When I use uri_to_assoc
, I always have a default array, so in my application, I can always get the required parameter, even if it missing from the uri
$param_default = array('cat','page');
$param_array = $this->uri->ruri_to_assoc(3, $param_default);
Now I can safely access $param_array['cat']
and $param_array['page']
even when uri doesn't contain that parameter.
I always user ruri_to_assoc
and ruri_segment
, so the extra parameter always start in 3rd uri
segment.
精彩评论