0

Im new for Magento. I'm displaying latest added products using following code,

<?php
$_productCollection = Mage::getResourceModel('reports/product_collection')
                    ->addAttributeToSelect('*')
                    ->setVisibility(array(2,3,4))                   
                    ->setOrder('created_at', 'desc')
                    ->setPage(1, 6);
?>

I need to display pagination on this page.

Any-one tell simple code/idea for this?

KarSho
  • 5,699
  • 13
  • 45
  • 78
  • Are you want pagination in gird style like on front side for products ? – Mahmood Rehman Nov 29 '13 at 05:24
  • no not like that. i need just like http://designbasement.org/wp-content/uploads/2012/06/minimal-pagination-540x304.png – KarSho Nov 29 '13 at 05:27
  • If you want to implement paginatin than you have to override paginatin block.second thing is your styling.this is just styled pagination nothing else. – Mahmood Rehman Nov 29 '13 at 05:30
  • But, i need implement this in my own page. I created, letest.phtml. I want there. – KarSho Nov 29 '13 at 05:32
  • Check these links for pagination implementation.after that if you feel still help we can help you.[http://www.excellencemagentoblog.com/magento-collection-paging] and [http://stackoverflow.com/questions/18664130/magento-paginate-a-custom-collection] – Mahmood Rehman Nov 29 '13 at 05:33

1 Answers1

2

Here is the following steps required to be followed -

Step1: Controller : First in in our IndexController we will simply load the layout and render it.

<?php
class Test_Collection_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {

        $this->loadLayout();     
        $this->renderLayout();
    }
}

Step2: Layout : In our module layout file, i.e collection.xml this is the code to put

<?xml version="1.0"?>
<layout version="0.1.0">
    <collection_index_index>
        <reference name="content">
            <block type="collection/collection" name="collection" template="collection/collection.phtml" />
        </reference>
    </collection_index_index>
</layout> 

Step3: Block : Next we will create our block ‘collection/collection’ where the main code for paging goes.

<?php
class Test_Collection_Block_Collection extends Mage_Core_Block_Template
{

    public function __construct()
    {
        parent::__construct();
        $collection = Mage::getModel('collection/collection')->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');
    }
}

As you can see in the above code we have created a block for the pager and set our collection in it.

Step4: phtml file : Next in our collection.phtml file we need to put in this code

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<?php $collection = $this->getCollection(); ?>
<div class="page-title">
    <h1><?php echo $this->__('My Collection') ?></h1>
</div>
<?php echo $this->getPagerHtml(); ?>
<?php if($collection->getSize()): ?>
<table class="data-table" id="my-custom-table">
    <col width="1" />
    <col width="1" />
    <col />
    <col width="1" />
    <col width="1" />
    <col width="1" />
    <thead>
        <tr>
            <th><?php echo $this->__('ID #') ?></th>
            <th><?php echo $this->__('Title') ?></th>
            <th><span class="nobr"><?php echo $this->__('Created') ?></span></th>
        </tr>
    </thead>
    <tbody>
        <?php $_odd = ''; ?>
        <?php foreach ($collection as $_obj): ?>
        <tr>
            <td><?php echo $_obj->getCollectionId() ?></td>
            <td><span class="nobr"><?php echo $_obj->getTitle(); ?></span></td>
            <td><?php echo $this->formatDate($_obj->getCreatedTime()) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<script type="text/javascript">decorateTable('my-custom-table');</script>
<?php echo $this->getPagerHtml(); ?>
<?php else: ?>
    <p><?php echo $this->__('The collection is empty.'); ?></p>
<?php endif ?>

This is all that is required, now the paging should show up on your collection.

aforankur
  • 1,291
  • 1
  • 15
  • 27