Skip to content

Instantly share code, notes, and snippets.

@bholota
Created November 23, 2012 07:33
Show Gist options
  • Save bholota/4134386 to your computer and use it in GitHub Desktop.
Save bholota/4134386 to your computer and use it in GitHub Desktop.
CakePHP pagination with component Paginator
//If we will use Paginator component in many actions add it to $components field:
public $components = array('Paginator');
//otherwise do it locally, inside action:
$this->Paginator = $this->Components->load('Paginator');
//optionally we can set conditions etc. (like in finding):
$this->Paginator->settings = array(
'limit' => 10, //objects per page
'conditions' => array(
'Quote.accepted' => 1
),
'order' => array('Quote.id' => 'desc')
);
//now we can put paginated object (default model) to view:
$this->set('quotes', $this->Paginator->paginate());
//or like that if we are using other model (loaded before):
$this->set('quotes', $this->Paginator->paginate('Quote'));
//in view like with auto generated index view:
<div class="quotes index" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
<h2><?php echo __('Latest'); ?></h2>
<ul>
<?php
foreach ($quotes as $quote):
?>
</br></br>
<li>
<p>#<?php echo h($quote['Quote']['id'])." ".$this->Html->link('+', array('action' => 'roxQuote', $quote['Quote']['id']))." (".$quote['Quote']['points'].") ".$this->Html->link('-', array('action' => 'suxQuote', $quote['Quote']['id']))."\t".$quote['Quote']['created']; ?></p>
<pre><?php echo h($quote['Quote']['content']); ?></pre>
</li>
<?php endforeach; ?>
</ul>
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment