Cakephp undefined variable paginator
When you create first pagination script, getting this error in quite normal, the error appears is due to cake not able to receive original parameter passed through controller and action. I am explaining this with example.
Without confusing you very much I will take you through the same example from cakephp manual and show you what is missing there.
Your controller
class RecipesController extends AppController {
var $paginate = array( 'limit' => 25, 'order' => array( 'Post.title' => 'asc' ) ); }
Action inside controller
function list_recipes() {
$data = $this->paginate('Recipe'); $this->set('data', $data); }
Your view file recipe.ctp
<?php echo $paginator->numbers(); ?> //Shows links to remaining pages
<?php
print_r($data); //To keep output simple
?>
This method works fine when you are displaying rows from recipes table without any conditions but when you try to give any conditions by changing your function something below
function list_recipes($type) {
$data = $this->paginate('Recipe', array(‘Recipe.type’ => $type)); $this->set('data', $data); }
Here I have added one parameter in action called $type so only recipes of a particular type will be fetched. Now here comes the problem your default view page works fine with numbers of links generating how many pages are available to click. But when you click next page link or more you get error
undefined variable paginator in view
This is due to your original parameter $type is not getting passed along with page number. You need to now pass both the variables via pagination, first $type & second is page number (page number is passed automatically). This can be done by
Chaning pages list line
from <?php echo $paginator->numbers(); ?>
To <?php echo $paginator->numbers(array(‘url’ => $type)); ?>
Now this will work perfectly only thing to make sure that in controller you have set view variable $type for access in view