Can someone please give a noob tutorial, step by step how to run php scripts. I need to run the script located here: Categories - how to display in alphabetical order? (Programmatically) But I cannot find anywhere on the internet, how to run this script. From what I understand you make a .php with the script in it. But what then? how do you run? through putty? through the browser?? can someone give specific instructions how this works. Tell me it like im 5 years old. Thank you.

  • In this case, SSH (i.e. Putty) is the right way: run php categories.php in the directory where you put the file. Dec 28 '15 at 9:11

Follow bellow steps

Step : 1 Fist create categories.php file on the magento root folder

Step : 2 After add bellow code in file categories.php.

ini_set('display_errors', 1);
class CAT

    public function index()
        //set the initial parameters for the function
        $root = 1;
        $conn = Mage::getSingleton('core/resource')->getConnection('core_write');
        //sort categories
        $this->orderCategories($root, $conn);

    public function orderCategories($parentId, $conn)
        //The position field is in the main category table
        //add prefix if you have one
        $table = 'catalog_category_entity';
        //get the children for a specific category id
        $collection = Mage::getModel('catalog/category')->getCollection()
            ->addFieldToFilter('parent_id', $parentId);
        $categories = array();
        foreach ($collection as $category){
            //in case there are 2 categories with the same name we shouldn't skip them
            //but there shouldn't be
            $categories[$category->getName().'_'.$category->getId()] = $category->getId();
        //sort categories by name
        //set their position
        $position = 1;
        foreach ($categories as $name=>$id){
            $q = "UPDATE {$table} SET `position` = {$position} where `entity_id` = {$id}";
            //sort the current category children by calling the same function recursively
            $this->orderCategories($id, $conn);

$obj = new CAT();

Step : 3 Run categories.php file in your browser for eg. http://magentohost.com/categories.php

  • 1
    Thank you so much. A programmer was planning to charge 100$ to fix this problem for me. You saved my wallet. Jan 7 '16 at 1:07
  • You can also extend the Mage_Shell_Abstract class which gives you a way to parse parameters and loads up magento for you.
    – Domino
    Mar 31 '17 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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