×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: bejoy balan
Added: Sep 25, 2014 6:49 AM
Views: 158
Apply pagination to magento collection,
  1. class Test_Featuredsalons_Block_Featuredsalons extends Mage_Core_Block_Template
  2. {
  3.  
  4.     public function __construct()
  5.     {
  6.         parent::__construct();
  7.         $collection = Mage::getModel('featuredsalons/featuredsalons')->getCollection();
  8.         $this->setCollection($collection);
  9.     }
  10.  
  11.     protected function _prepareLayout()
  12.     {
  13.         parent::_prepareLayout();
  14.  
  15.         $pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
  16.         $pager->setCollection($this->getCollection());
  17.         $this->setChild('pager', $pager);
  18.         $this->getCollection()->load();
  19.  
  20.         return $this;
  21.     }
  22.  
  23.     public function getPagerHtml()
  24.     {
  25.         return $this->getChildHtml('pager');
  26.     }
  27.  
  28.     public function getCollection()    
  29.      {            
  30.         $limit      =   10;
  31.         $curr_page  =   1;
  32.  
  33.         if(Mage::app()->getRequest()->getParam('p'))
  34.         {
  35.             $curr_page  =   Mage::app()->getRequest()->getParam('p');
  36.         }
  37.         //Calculate Offset  
  38.         $offset     =   ($curr_page - 1) * $limit;
  39.  
  40.         $collection =   Mage::getModel('featuredsalons/featuredsalons')->getCollection()
  41.                                                     ->addFieldToFilter('status',1);    
  42.         $collection->getSelect()->limit($limit,$offset);
  43.  
  44.         return $collection;
  45.     }  
  46. }