Tags

, , , , , ,


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.

Advertisements