how to send my sql to use magento pager

149 views Asked by At

I have created a new database to give a list of custom order list

i give database to show with SQL code PS $sql is multiple line

  $sql = select * from database_name WHERE customfilter like $customfilter limit $startfrom, $limit
    $result = mysqli_query($cn,$sql)
    $collection = mysqli_fetch_array($result)

and I don't know which code to edit. i tried to edit code on product page to use in this page but it gives error.

Can anyone please suggest me which code to edit to use pager.

Below is the code that i tried

$this->getCollection($collection);
$pager = $this->getLayout()->createBlock('page/html_pager', 'ordercustom.pager')
        ->setCollection($this->getCollection()); 
    $this->setChild('pager', $pager);
    return $this;

but it displays error.

2

There are 2 answers

2
Venkat On BEST ANSWER

Please have look on below code for product collection and setting the limit for page size.

$collection  = Mage::getModel ('catalog/product')
                    ->getCollection()                       -
                    ->addAttributeToSelect('*')
                    ->setPageSize($this->getRequest()->getParam("limit"))
                    ->load();

Please adopt customer collection page size from above and let me know.

0
Tethys On
$collection  = Mage::getModel ('customorder/index')
                        ->getCollection()
                        ->addAttributeToSelect('*')
                        ->addAttributeToFilter('customer_code', $customercode)
                        ->addAttributeToFilter('shipping', array('eq' => $shipping))
                        ->setPageSize($this->getRequest()->getParam("limit"))
                        ->load();
            $this->setCollection($collection);

It displays Fatal error: Call to a member function getCollection() on a non-object

PS

customorder/index is in my customorder.xml

<block type="customorder/index" name="customorder" template="customorder/index.phtml"/>

PS

My database is not original model from magento, its filename is customorder.

** on my try pages code**

  1. First have a look on the code

    public function __construct(){

        parent::__construct();
        $collection  = Mage::getModel ('ordercustom/index')
        ->getCollection()                       -
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('customer_code', array('eq' => $customercode))
        ->addAttributeToFilter('shipping', array('eq' => $shipping))
        ->setPageSize($this->getRequest()->getParam("limit"))
        ->load();
        $this->setCollection($collection);
        } 
    
    protected function _prepareLayout()
            {
                parent::_prepareLayout();
    
            $pager = $this->getLayout()->createBlock('page/html_pager', 'customorder.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');
            } 
    
    1. And on the frontend

echo $this->getPagerHtml();