For creating pagination in zend framework 1 you have to perform several steps:
1) Set up pagination template.
Create file application/views/scripts/pagination.phtml:
For instance, you can create method getPaginator in your table model:
Add to application.ini:
Add in view template:
1) Set up pagination template.
Create file application/views/scripts/pagination.phtml:
<div> <ul class="pagination"> <?php if (isset($this->previous)): ?> <li><a href="<?= $this->url(array('page' => $this->first)); ?>"><span>«</span></a> <?php else: ?> <li class="disabled"><span>«</span></li> <?php endif; ?> <?php if (isset($this->previous)): ?> <li><a href="<?= $this->url(array('page' => $this->previous)); ?>">←</a></li> <?php else: ?> <li class="disabled"><span>←</span></li> <?php endif; ?> <!-- Numbered page links --> <?php foreach ($this->pagesInRange as $page): ?> <?php if ($page != $this->current): ?> <li><a href="<?= $this->url(array('page' => $page)); ?>"><?= $page; ?></a></li> <?php else: ?> <li class="active"><span><?= $page; ?></span></li> <?php endif; ?> <?php endforeach; ?> <!-- Next page link --> <?php if (isset($this->next)): ?> <li><a href="<?= $this->url(array('page' => $this->next)); ?>">→</a></li> <?php else: ?> <li class="disabled"><span>→</span></li> <?php endif; ?> <!-- Last page link --> <?php if (isset($this->next)): ?> <li><a href="<?= $this->url(array('page' => $this->last)); ?>">»</a></li> <?php else: ?> <li class="disabled"><span>»</span></li> <?php endif; ?> </ul> </div>2) Create an instance of Zend_Paginator with paginator adapter.
For instance, you can create method getPaginator in your table model:
/** * Class Application_Model_DbTable_Task */ class Application_Model_DbTable_Task extends Zend_Db_Table_Abstract { protected $_name = 'task'; protected $_rowClass = 'Application_Model_Task'; public function getPaginator(array $where = []) { $db = $this->getAdapter(); $select = $db->select(); $select->from($this->_name); foreach ($where as $key => $value) { $select->where($key, $value); } $select->order('created_at DESC'); $adapter = new Zend_Paginator_Adapter_DbSelect($select); $paginator = new Zend_Paginator($adapter); return $paginator; } }and in your controller:
$user = Application_Model_User::getCurrent(); $tableTask = new Application_Model_DbTable_Task(); $paginator = $tableTask->getPaginator(['id_user = ?' => $user->id]); $page = $this->getRequest()->getParam('page', 1); $paginator->setCurrentPageNumber($page); $config = $this->getInvokeArg('bootstrap')->getOptions(); $paginator->setItemCountPerPage($config['pagination']['per_page']); $this->view->paginator = $paginator;3) Configure pagination in settings file and in your bootstrap.
Add to application.ini:
pagination.per_page = 10and in bootstrap:
protected function _initPagination() { Zend_Paginator::setDefaultScrollingStyle('Sliding'); Zend_View_Helper_PaginationControl::setDefaultViewPartial( 'pagination.phtml' ); }4) Print pagination html.
Add in view template:
<?php if (count($this->paginator)): ?>where $item - a row from db
<?php foreach ($this->paginator as $item): ?>
<div class="item">
<?= $item['value'] ?> (<?= $item['state'] ?>)
</div>
<?php endforeach; ?>
<?php else: ?>
<div class="empty">
Nothing is found.
</div>
<?php endif; ?>
<?= $this->paginator ?>
No comments:
Post a Comment