Create custom pagination on custom module in Magento

By | April 17, 2014

Here, Some time we need custom pagination instead of Magento default pagination.

So, here is a snippet example code that would help you to create pagination with custom collection on custom module in Magento.

Step 1: First paste the following code in your block class (e.g. testimonial.php)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

    private $_itemPerPage = 2;
    private $_pageFrame = 8;
    private $_curPage = 1;
    
    public function getCollection($collection = 'null')
    {
        if($collection != 'null'){
            $page = $this->getRequest()->getParam('p');
            if($page) $this->_curPage = $page;
            
            $collection->setCurPage($this->_curPage);
            $collection->setPageSize($this->_itemPerPage);
            return $collection;
        }
    }
    
    public function getPagerHtml($collection = 'null')
    {    
        $html = false;
        if($collection == 'null') return;
        if($collection->count() > $this->_itemPerPage)
        {
            $curPage = $this->getRequest()->getParam('p');
            $pager = (int)($collection->count() / $this->_itemPerPage);
            $count = ($collection->count() % $this->_itemPerPage == 0) ? $pager : $pager + 1 ;
            $url = $this->getPagerUrl();
            $start = 1;
            $end = $this->_pageFrame;
            
            $html .= '<ol>';
            if(isset($curPage) && $curPage != 1){
                $start = $curPage - 1;                                        
                $end = $start + $this->_pageFrame;
            }else{
                $end = $start + $this->_pageFrame;
            }
            if($end > $count){
                $start = $count - ($this->_pageFrame-1);
            }else{
                $count = $end-1;
            }
            
            for($i = $start; $i<=$count; $i++)
            {
                if($i >= 1){
                    if($curPage){
                        $html .= ($curPage == $i) ? '<li class="current">'. $i .'</li>' : '<li><a href="'.$url.'&p='.$i.'">'. $i .'</a></li>';
                    }else{
                        $html .= ($i == 1) ? '<li class="current">'. $i .'</li>' : '<li><a href="'.$url.'&p='.$i.'">'. $i .'</a></li>';
                    }
                }
                
            }
            
            $html .= '</ol>';
        }
        
        return $html;
    }
    
    public function getPagerUrl()   // You need to change this function as per your url.
    {
        $cur_url = mage::helper('core/url')->getCurrentUrl();
        $new_url = preg_replace('/\&p=.*/', '', $cur_url);
        
        return $new_url;
    }

?>

Setp 2: Now you have to modify your frontend phtml page. (e.g. testimonial.phtml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//pagination on top of contents
<?php if($this->getPagerHtml($this->getProductCollection())):?>        // pass the default collection as parameter
<div class="pages">
    <span><?php echo $this->__('Page : ');?></span>
    <?php echo $this->getPagerHtml($this->getProductCollection());?>
</div>
<?php endif;?>
 
<?php
    $_collections = $this->getProductCollection();            // Default Collection on which you'll implement pagination.
    $_productCollection = $this->getCollection($_collections);  // calling the function that have been created in block page.
    foreach ($_productCollection as $_product):

   ------------------ Your code here ------------

   endforeach;
?>    

//pagination on bottom of contents
<?php if($this->getPagerHtml($this->getProductCollection())):?>        // pass the default collection as parameter
<div class="pages">
    <span><?php echo $this->__('Page : ');?></span>
    <?php echo $this->getPagerHtml($this->getProductCollection());?>
</div>
<?php endif;?>

Hope! you will enjoy :-) !!!

Leave a Reply