Add Custom Pagination in Magento

Add following code in block PHP file.

protected $_pagesCount = null;
protected $_currentPage = null;
protected $_itemsOnPage = 10;
protected $_itemsLimit;
protected $_pages;
protected $_displayPages   = 5;

protected function _construct()
{
$this->_currentPage = $this->getRequest()->getParam(‘page’);
if (!$this->_currentPage) {
$this->_currentPage=1;
}

$itemsPerPage = 5;
if ($itemsPerPage > 0) {
$this->_itemsOnPage = $itemsPerPage;
}
}

public function getNewsList()
{
$collection = Mage::getModel(‘news/news’)->getCollection();

if ($this->_itemsLimit!=null && $this->_itemsLimit<$collection->getSize()) {
$this->_pagesCount = ceil($this->_itemsLimit/$this->_itemsOnPage);
} else {
$this->_pagesCount = ceil($collection->getSize()/$this->_itemsOnPage);
}
for ($i=1; $i<=$this->_pagesCount;$i++) {
$this->_pages[] = $i;
}
$this->setLastPageNum($this->_pagesCount);

$offset = $this->_itemsOnPage*($this->_currentPage-1);
if ($this->_itemsLimit!=null) {
$_itemsCurrentPage = $this->_itemsLimit – $offset;
if ($_itemsCurrentPage > $this->_itemsOnPage) {
$_itemsCurrentPage = $this->_itemsOnPage;
}
$collection->getSelect()->limit($_itemsCurrentPage, $offset);
} else {
$collection->getSelect()->limit($this->_itemsOnPage, $offset);
}
return $collection;

}

public function isFirstPage()
{
if ($this->_currentPage==1) {
return true;
}
return false;
}

public function isLastPage()
{
if ($this->_currentPage==$this->_pagesCount) {
return true;
}
return false;
}

public function isPageCurrent($page)
{
if ($page==$this->_currentPage) {
return true;
}
return false;
}

public function getPageUrl($page)
{
return $this->getUrl(‘*’, array(‘page’ => $page));
}

public function getNextPageUrl()
{
$page = $this->_currentPage+1;
return $this->getPageUrl($page);
}

public function getPreviousPageUrl()
{
$page = $this->_currentPage-1;
return $this->getPageUrl($page);
}

public function getPages()
{
$collection = Mage::getModel(‘news/news’)->getCollection();
$pages = array();
if ($this->_pagesCount <= $this->_displayPages) {
$pages = range(1, $this->_pagesCount);
}
else {
$half = ceil($this->_displayPages / 2);
if ($this->_currentPage >= $half && $this->_currentPage <= $this->_pagesCount – $half) {
$start  = ($this->_currentPage – $half) + 1;
$finish = ($start + $this->_displayPages) – 1;
}
elseif ($this->_currentPage < $half) {
$start  = 1;
$finish = $this->_displayPages;
}
elseif ($this->_currentPage > ($this->_pagesCount – $half)) {
$finish = $this->_pagesCount;
$start  = $finish – $this->_displayPages + 1;
}

$pages = range($start, $finish);
}
return $pages;
//return $this->_pages;
}

 

Now , add following code into phtml file.

 

<table>
<?php foreach($this->getNewsList() as $item): ?>
<tr>
<td><img src=”<?php echo Mage::getBaseUrl(‘media’) . ‘news’. ‘/’ . $item[‘filename’] ?>” alt=”<?php echo $item[‘title’]
?>” width=”64″ height=”64″ style=”border: 1px solid
#d5d5d5;padding:3px;margin-right: 10px;” /></td>
<td>
<h5><?php echo $item[‘title’] ?></h5>
<p><?php echo $this->limitCharacter($item[‘content’], 180, ”
<a href='”.Mage::getBaseUrl().”news/index/view/id/”.$item[‘news_id’].”‘>read more</a>”)?></p>
</td>
</tr>
<?php endforeach; ?>
</table>

<?php if($this->getLastPageNum()>1): ?>
<div>
<p>
<?php if (!$this->isFirstPage()): ?>
<a href=”<?php echo $this->getPreviousPageUrl() ?>”><?php echo $this->__(‘Previous’) ?></a>
<?php endif;?>
<?php foreach ($this->getPages() as $_page): ?>
<?php if ($this->isPageCurrent($_page)): ?>
<span><?php echo $_page ?></span>
<?php else: ?>
<a href=”<?php echo $this->getPageUrl($_page) ?>”><?php echo $_page ?></a>
<?php endif ?>
<?php endforeach; ?>
<?php if (!$this->isLastPage()): ?>
<a href=”<?php echo $this->getNextPageUrl() ?>”><?php echo $this->__(‘Next’) ?></a>
<?php endif ?>
</p>
</div>
<?php endif;?>

 

Thats it!!!

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: