Tags
argument, CakePHP, GET, pagination, paginator, parameter passed, PHP
For searching we many need to pass variable like ?keyword=test, and we may need to add pagination withen the search, Here I describe multiple cases where we may need to use pagination and how we manage the url for pagination when we make some searching
Case 1: if the url like the following
http://www.mydomain.com/controller/method/?keyword=test1&opt=test2
In this case we need to add the paginator option like this
First we retrive the passing variable and assign value, we can get it from “$this->params[‘url’]
$urls = $this->params['url']; $getv = ""; foreach($urls as $key=>$value) { if($key == 'url') continue; // we need to ignor the url field $getv .= urlencode($key)."=".urlencode($value)."&"; // making the passing parameters } $getv = substr_replace($getv ,"",-1); // remove the last char '&'
Now we have the all get parameter, lest assign this to paginator option
$paginator->options(array('url' => array("?"=>$getv)));
Now all the pagination links always taking this get parameter so now if we gos to the next page the url look like this
http://www.mydomain.com/controller/method/page:2?keyword=test1&opt=test2
Now If we need passing an argument as id like the following
http://www.mydomain.com/controller/method/1/?keyword=test1&opt=test2
where 1 is an id or something we get this 1 from $this->passedArgs[0] So now we have to set the paginator option like this
$paginator->options(array('url' => array($this->passedArgs[0],"?"=>$getv)));
If we have passed more than one argument, we need to take it
$pass_argument = $this->passedArgs[0]."/"$this->passedArgs[1]."/".$this->passedArgs[2]."/"; $paginator->options(array('url' => array($pass_argument,"?"=>$getv)));
It depend on you how much you want to passed the argument, if you faced any problem print the $this->passedArgs and see how it organize the argument. Its really easy and I’ve already used it. Cheers
–new update
I got the idea from comments, here is update, the following code will useful for multiple arguments or params.
//extract the get variables $url = $this->params['url']; unset($url['url']); $get_var = http_build_query($url); $arg1 = array(); $arg2 = array(); //take the named url if(!empty($this->params['named'])) $arg1 = $this->params['named']; //take the pass arguments if(!empty($this->params['pass'])) $arg2 = $this->params['pass']; //merge named and pass $args = array_merge($arg1,$arg2); //add get variables $args["?"] = $get_var; $paginator->options(array('url' => $args));
done.. But I’m trying to make better solution, will update it soon.
It works!.Thank you so much
Thanks for this lovely trick
but if the keyword contains ‘#’ and ‘?’ sign then this trick fails completely …
any solution for this will be much appreciated
keyword=#topic
keyword=what+is?+topic
Thanks to mention about this. If you notice I have used urlencode to encode the keywords so the following should be
$keyword = ‘#topic’
it should be $keyword = %23topic [ as we used urlencode($keyword)]
and
$keyword = ‘what is? topic’
it should be after encoded $keyword = ‘what+is%3F+topic’
It not may case the problem i think, I just tested and it work with me properly. Please try and let me know if you find any other issues.
Hi,
Great job !!
Thanks!
You can use http_build_query for building your url instead foreach.
$getv = http_build_query($this->params[‘url’]);
Thanks, this helped doing pagination for a search function, where the search string could contain ‘/’ chars.
If you go the http_build_query($this->params[‘url’]); as grabriel suggested, you still need to remove the $this->params[‘url’][‘url’] variable or the pagination links fail. I resolved it by doing:
unset($this->params[‘url’][‘url’]);
$getv = http_build_query($this->params[‘url’]);
You can also use the cakephp Router method, queryString, to generate the $getv variable. (http://api13.cakephp.org/view_source/router/#line-1010)
unset($this->params[‘url’][‘url’]);
$getv = Router::queryString($this->params[‘url’]);
Hello, thank you for the idea, but your implementation is too weak.
For example, when I have my own ‘named’ parameters in the url this won’t work.
named pars are: http://example.com/index/named:value/something:2/
This combined with passed ones (non-named) and get values is too complicated for your solution.
I think mine does the trick:
//in app_controller:
//It returns a cake ‘url’ array, just pass it in the paginator options as ‘url’ => $url;
function __getUrlFromParams(){
$urlarr = array();
$urlarr[‘controller’] = $this->params[‘controller’];
$urlarr[‘action’] = $this->params[‘action’];
$urlarr = array_merge($urlarr, $this->params[‘named’]);
foreach($this->params[‘pass’] as $v){
$urlarr[] = $v;
}
$urlarr[‘plugin’] = $this->params[‘plugin’];
$url = $this->params[‘url’];
unset($url[‘url’]);
$urlPars = ”;
foreach($url as $k => $v){
if(empty($urlPars)){
$urlPars .= $k.’=’.$v;
}
else{
$urlPars .= ‘&’.$k.’=’.$v;
}
}
$urlarr[‘?’] = $urlPars;
return $urlarr;
}
Have fun.
thanks for your code but your code is too long, you can try the following, i tried to minimized code
$url = $this->params['url'];
unset($url['url']);
$get_var = http_build_query($url);
$arg1 = array(); $arg2 = array();
if(!empty($this->params['named']))
$arg1 = $this->params['named'];
if(!empty($this->params['pass']))
$arg2 = $this->params['pass'];
$args = array_merge($arg1,$arg2);
$args["?"] = $get_var;
$paginator->options(array('url' => $args));
You can add plugins or anything in this way.
thanks a lot! this works so good, you save me, thanks again!!!
Good stuff. Thanks.
Thank you very much. This bit of code helped me.
every one says $paginator->options(array(‘url’ => $this->passedArgs));
is the solution for this, even its on cakephp site. however this did not work for me. Can you explain plz?
why am I having empty $this->passedArgs, on my views
thanks
This could be a lot shorter, try:
$urlParams = $this->params[‘url’];
unset($urlParams[‘url’]);
$this->Paginator->options(array(‘url’ => array(‘?’ => http_build_query($urlParams))));
Works like a charm, thanks!
And thanks to Sarwar as well for this article, your code does the trick too.
Pingback: Programming Note : CakePHP’s paginator for Social Games (Japan) « Aru..fian
Pingback: like on facebook
thanks. it works
プレイボーイ ポロ
enjoy watching $this->Link