1

I list the subcategories with this code

$root = Mage::getModel('catalog/category')->load(3); // Put your category ID here.
$subCat = explode(',',$root->getChildren()); 
$collection  = $root->getCollection()->addAttributeToSelect("*")->addFieldToFilter("entity_id", array("in", $subCat) );
foreach($collection as $subcategory) { 
  echo '<a href="'.$subcategory->getURL() .'" />» '.$subcategory->getName().'</a><br/>';
}

I want to show only first 3 subcategories. How can I do it?

Dushyant Joshi
  • 3,672
  • 3
  • 28
  • 52
Gökhan YILDIZ
  • 131
  • 3
  • 16
  • possible duplicate of [Magento: Set LIMIT on collection](http://stackoverflow.com/questions/14322333/magento-set-limit-on-collection) – Mihai Stancu Mar 26 '14 at 09:09

3 Answers3

5

Use

$collection
    // ->addFieldToFilter...
    ->setPageSize(20)
    ->setCurPage(1);
Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
5

Try adding a limit to the collection.
And in addition you can get the root catalog id without hard coding it.
You also don't need all the root children ids. You can filter the collection by parent id.

$rootId = Mage::app()->getStore()->getRootCategoryId();
$collection  = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect("*")
            ->addFieldToFilter("parent_id", $rootId);
$collection->addAttributeToSort('position'); //sort by position
$collection->setPage(1, 3);//limit 3, page 1
foreach($collection as $subcategory) { 
   echo '<a href="'.$subcategory->getURL() .'" />» '.$subcategory->getName().'</a><br/>';
}
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
Marius
  • 15,148
  • 9
  • 56
  • 76
  • Hmmm...someone downvoted this right after I posted the answer. I don't think he even read the answer. Is this a rage down-vote? Anyway. I've tested this, and it works. – Marius Mar 26 '14 at 09:07
  • 1
    Thanks for the +1s. I know who downvoted this. It is the guy that had an answer here, but deleted it. He thought I downvoted his answer (which I didn't) because I said it's not the best solution. Well...there are people and people. – Marius Mar 26 '14 at 09:11
  • Actually I downvoted his answer and was explaining why in a long comment about php/mysql context switching, sql full table scans, sql memory usage, php memory usage. But he deleted his answer before I could complete my comment. It's a clear case of misdirected revenge downvote from dagerously oblivious developers. – Mihai Stancu Mar 26 '14 at 09:12
  • Based on what is written in the question this is the perfect answer. Right @MihaiStancu? – Dushyant Joshi Mar 26 '14 at 09:14
  • 2
    @MihaiStancu Then this was by accident. You should always drop a comment, give the person a chance to react on this, and then down-vote. not before. You can trust me that if there are good reasons, I'll change or delete my answer – hek2mgl Mar 26 '14 at 09:14
  • You're right. One question though where is the comment you left on @Marius's answer and what was the flaw in Marius's answer that your comment was targetting? – Mihai Stancu Mar 26 '14 at 09:17
  • 3
    I think that all 3 answers provided here are correct. (that's why I +1'd the other 2). I just took it up a notch and optimized the rest of the code. – Marius Mar 26 '14 at 09:17
  • @MihaiStancu I didn't commented any flaw on Mihai's answer. It's ok. All are ok and I've upvoted them all. And I admit that my answer was really bad (now where I see how you guys are doing it) – hek2mgl Mar 26 '14 at 09:20
4

Use following

$collection= Mage::getModel('catalog/category')
->getCollection()
->setPageSize(3);
echo $collection->count();
Dushyant Joshi
  • 3,672
  • 3
  • 28
  • 52