Filter ítems by category:
$this->Items->findAll(array('Item.category_id'=>$category_id));
1.2 pagination is easy. What I do is something like ($paginate is a member
variable in the controller that the paginator component uses to fetch the
parameters, the pagination helper will be auto-added for you):
$this->paginate['Item'] = array(
'limit' => 10,
'order' => array ('Item.name' => 'asc')
);
$records = $this->paginate('Item');
$this->set('records', $records);
Then on the view I have:
<?php $paginator->options(array('update' => 'content', 'indicator' =>
'spinner')); ?>
This at the top, together with having RequestHandler as a component in your
controller will let you enjoy AJAX pagination. Otherwise just omit above
line.
Still on the view:
<?php echo $paginator->prev('<< Previous', (isset($paginator_params)
? $paginator_params : null), null, array('class' => 'disabled')); ?>
<?php echo $paginator->next('Next >>', (isset($paginator_params) ?
$paginator_params : null), null, array('class' => 'disabled')); ?>
Page <?php echo $paginator->counter(array('separator' => ' of '));
?>
Sort by: ><?php echo $paginator->sort('Name', 'name'); ?>
<?php pr($records); ?>
Now on the controller the $paginationParameters that I've set I added it
because sometimes our pagination should come after a parameter to the action
was sent. Let's say that your action may (not necessary) receive the
category_id so you could just paginate the items belonging to the category.
This is what I do (may not be the perfect way but it is what I've figured):
On the controller I add the parameter to the action, as optional:
function list($category_id = null)
Then at the very top if $post is NOT set, I may find it as a parameter as a
result of a pagination, so I do:
if (isset($this->params['category_id']) &&
isset($this->params['pass']['category_id']))
{
$category_id = $this->params['pass']['category_id'];
}
Now I use the $paginationParameters above and set its post if it was set
(this is just before calling $this->paginate()):
if (isset($category_id))
{
$paginationParameters['category_id'] = $category_id;
}
So altogether the controller action looks like:
function list($category_id = null)
{
if (isset($this->params['category_id']) &&
isset($this->params['pass']['category_id']))
{
$category_id = $this->params['pass']['category_id'];
}
$paginationParameters = array();
$paginationParameters['controller'] = 'items'; // Your current
controller
$paginationParameters['action'] = $this->action;
if (isset($category_id))
{
$paginationParameters['category_id'] = $category_id;
}
$this->paginate['Item'] = array(
'limit' => 10,
'order' => array ('Item.name' => 'asc'),
'url' => array ('url' => $paginationParameters)
);
$records = $this->paginate('Item');
$this->set('records', $records);
$this->set('pagination_params',
$paginationParameters['Item']['url']);
}
Something like that.
-MI
---------------------------------------------------------------------------
Remember, smart coders answer ten questions for every question they ask.
So be smart, be cool, and share your knowledge.
BAKE ON!
blog: http://www.MarianoIglesias.com.ar
-----Mensaje original-----
De: [email protected] [mailto:[EMAIL PROTECTED] En nombre
de Mech7
Enviado el: Viernes, 16 de Febrero de 2007 12:47 p.m.
Para: Cake PHP
Asunto: Categories?
How would i filter the items based on category when a menu item has
been clicked ? And also how does the pagination work in 1.2 ?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---