2

I had displayed images from my custom module database on modules template. and applied pagination on it. on frontend it showing the pagination block but pagination is not working it shows all images.

here is code of my block file:-

public function __construct()
{
    parent::__construct();
    $collection = Mage::getModel('brand/brand')->getCollection();
    $this->setCollection($collection);
}

protected function _prepareLayout()
{
    parent::_prepareLayout();

    $pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
        $pager->setAvailableLimit(array(5=>5,10=>10,20=>20,'all'=>'all'));
        $pager->setCollection($this->getCollection());
        $this->setChild('pager', $pager);
        $this->getCollection()->load();
        return $this;
    }

    public function getPagerHtml()
    {
    return $this->getChildHtml('pager');
    }

and my phtml file code is:

<?php
$data = Mage::getModel('brand/brand')->getCollection();
$name =$data->getData('title');
$image = $data->getData('filename');
echo $this->getPagerHtml();
foreach ($data as $dt) 
{
    $name =$dt['title'];
    $image = $dt['filename'];



    echo $frontimage= '<img height="60px" width="80px" src="'.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).$image . '" />;
}



?>
1

This is your problem.
In the template you have this line:

$data = Mage::getModel('brand/brand')->getCollection();

then you iterate through the $data values.
In this case, the pagination does not affect your collection, because you are initializing your collection again.
Make your template file look like this:

$data = $this->getCollection();
echo $this->getPagerHtml();
foreach ($data as $dt) 
{
    $name =$dt['title'];
    $image = $dt['filename'];
    echo $frontimage= '<img height="60px" width="80px" src="'.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).$image . '" />;
}

This way you will use the collection initialized in the block and processed by the pager.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.