1

I have created a custom table in my magento store and wants to show its data on frontend.

For this I have created a block file in my custom module like :

class Custom_Module_Block_Pagination extends Mage_Core_Block_Template
{

    public function __construct()
    {
        parent::__construct();
        $customer = Mage::getSingleton('customer/session')->getCustomer();
        $readConnection = Mage::getSingleton('core/resource')->getConnection('core_read');
        $rows = $readConnection->fetchAll('Select * from customer_table where c_id = '.$customer->getId().'');

        $collection = new Varien_Data_Collection();
        foreach($rows as $row){
            $rowObj = new Varien_Object();
            $rowObj->setData($row);
            $collection->addItem($rowObj);
        }
        $this->setCollection($collection);


        /* // this collection works perfect with pagination 
        $collection = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*');
        $this->setCollection($collection);
        */
    }

     protected function _prepareLayout()
    {
        parent::_prepareLayout();
        $pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
        $pager->setAvailableLimit(array(10=>10,20=>20));
        $pager->setCollection($this->getCollection());
        $this->setChild('pager', $pager);
        $this->getCollection()->load();
        return $this;
    }

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

On frontend the collection not working with pagination.

For example : the collection return 43 rows and pagination showing number of 5 pages in toolbar with Row per page is 10 but collection showing all 43 rows on all pages. but it should showing only 10 rows on all pages.

Why this is happened? How to resolve this?

EDIT : My template file :

$collection = $this->getCollection();
echo $this->getToolbarHtml();
foreach($collection as $row)
{
    // here tables's tbody
}
4

The issue is related to Magento's Varien_Data_Collection It does not support pagination. As you can see an answer here and also a possible solution here
You can always use a Model for getting collection.
EDIT
One more reference to look at.

|improve this answer|||||
0

Here i tell what is scenario here you add adding the total collection to pager block.

public function __construct()
  {
     $this->setCollection($collection);
  }

also set the pager configuration and pager block too

protected function _prepareLayout()
{
    parent::_prepareLayout();
    $pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
    $pager->setAvailableLimit(array(10=>10,20=>20));
    $pager->setCollection($this->getCollection());
    $this->setChild('pager', $pager);
    $this->getCollection()->load();
    return $this;
}

and getting the pager block too

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

but here you missed the collection with limits

public function getCusCollection()
{
  //get values of current page
    $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
//get values of current limit
    $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 5;
    $offset = ($page - 1) * $pageSize;


    $customer = Mage::getSingleton('customer/session')->getCustomer();
    $readConnection = Mage::getSingleton('core/resource')->getConnection('core_read');
    $rows = $readConnection->fetchAll('Select * from customer_table where c_id = '.$customer->getId().' LIMIT '.$offset.','.$pageSize.'');

    $collection = new Varien_Data_Collection();
    foreach($rows as $row){
        $rowObj = new Varien_Object();
        $rowObj->setData($row);
        $collection->addItem($rowObj);
    }
    return $collection;
}

in your phtml file you call the filtered collection

 $collection = $this->getCusCollection();
echo $this->getToolbarHtml();
foreach($collection as $row)
{
    // here tables's tbody
}
|improve this answer|||||

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.