tags:

views:

71

answers:

2

Hello All!

In my magento project..I want to sort all product category according to custom_design_from or custom_design_to....

like

latest added category must be display at first in a row than so on..as according to stack concept

can anyone please help me out..how can I do display categories on the page.

Thanks! Richa

A: 

In simple PHP script it can be done by simply using MySql Select Query.

Use this SQL Select Query According to Megento Pattarn :

mysql_query("select col_name from table_name ORDER BY time_col_name DESC");

Check this hope it will helpfull to you.

Ricky Dang
Thanks for the noticing but magneto pattern is totally different than simple php database..
Richa
i know its diffrent thats why i hav told u that use This query in magento pattern
Ricky Dang
+2  A: 

Hi, if you are looking to grab all of the categories in your site and order them by the date they were created then you can use something along the lines of:

$categoryCollection = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->setOrder('created_at', 'DESC')
->load();

You will probably want to replace ->addAttributeToSelect('*') by explicitly stating the attributes you want back - just to speed things up a little.

I have used DESC here as per your request but you could also use ASC for the opposite effect.

The code above will give you a whole collection. To get to the actual categories you will need to iterate over that collection with something like the following:

foreach($categoryCollection as $category) {
echo($category->getCreatedAt() . "<br/>");
}

This should give you a nicely printed out list of dates that each category was created.

To get more info on each category you can use things like $category->getName(), $category->getId() and so on. You get the idea.

This code will more than likely go in your block or helper and you can wrap it in a function which you will be able to call from your template to get access to the required category information.

Hopefully this helps.

Drew