How to use pagination in Magento

classYour_Module_Block_Entityname_ListextendsMage_Core_Block_Template
{    protected  function _construct(){
     // We get our collection through our model
        parent::_construct();
      // Instantiate a new Pager block
        $this->_entities =Mage::getModel('your_module/entityname')->getCollection()->setOrder('created_at', 'desc');
      // /! The limit must be set before the collection
        $pager =newMage_Page_Block_Html_Pager();// We set our limit (here an integer store in configuration).
      // Add our Pager block to our current list block 
        $pager
            ->setLimit((int)Mage::getStoreConfig('your_module/entityname/pagination'))->setCollection($this->_entities);
        $this->setChild('pager', $pager);
    }
}

You just need now to include the call in your template (phtml) file :

<divclass="your_module_entities">
  <?php foreach($this->_entities as $entity):?>
      <divclass="entity">
           <h2>
               <?php echo $entity->getAttribute1();?>
           </h2>
           <p>
                <?php echo $entity->getAttribute2();?>
           </p>
        </div>
   <?php endforeach;?></div><?php echo $this->getChildHtml('pager');?>
 
作者:冯亮
         
能力有限,水平一般。如有错误,欢迎指正
原文地址:https://www.cnblogs.com/fengliang/p/3660635.html