phpcakephpcakephp-1.3

go to page form for cakephp paginator


I have my paginator setup and it is working charmingly. Now I need a go to page form to jump to a page based on its number directly. I was wondering if it could be done just by setting a right name for my input field which takes the page number, or I have to do some additional myself?

I'm using cakephp 1.3


Solution

  • The easiest way to do what you are asking is to have a form that submits with the GET method and an input named "page":

    <form method="get" action="/mycontroller/myaction/">
       <input type="text" name="page" />
       <input type="submit" />
    </form>
    

    This will append ?page=[page number] to your url. However if you want to have nice urls the other way is to write code in your controller to redirect using named parameters which is how the normal Cake pagination methods work:

    Controller:

    if (!empty($this->params['url']['page'])) {         
    $url = array('controller'=>'mycontroller', 'action' => 'myaction');
    $url = array_merge($url, array('page'=>$this->params['url']['page']));          
    $this->redirect($url);
    }   
    

    Probably a good idea to also make sure the text input value does not contain malicious code by using Cake's Sanitize functions.