2

Whats the best way to get the Next and Previous Item from a collection?

1

2 Answers 2

2

To Get Next and Previous item from a collection you need to Iterate collection object

For Example

foreach($collection as $_collection){
    print_r($_collection->getData())
}

If you want to get only first or last item from collection,methods are available for that:

$collection->getFirstItem() and $collection->getLastItem()

2

Collection itself

As Minesh pointed out indirectly: There is no way using the API of the colletion directly.

The problem is, that magento only implements IteratorAggregate, so the Collection itself doesn't offer what you want.

The Iterator is an ArrayIterator

public function getIterator()
{
    $this->load();
    return new ArrayIterator($this->_items);
}

But even with it, you don't have a previous method, but a lot of other methods, have a look in the doc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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