Question

I am Getting the sub categories of current categories but it is showing in random way or may be according to the category ID as follows:

  <?php
  $category_ido = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
  $categories = Mage::getModel('catalog/category')->load($category_ido)->getChildren();
  $catArray = explode(',', $categories); ?>
      <?php if($categories) { ?>
      <div class="category-products category-products-sub">
          <ul class="products-grid">
              <?php
                foreach($catArray as $child)
                  {
                $_child = Mage::getModel( 'catalog/category' )->load( $child ); ?>

        <li class="item <?= $class ?> sub-cat-items">
        <a href="<?php echo $_child->getURL() ?>" title="<?php echo   $this->
        htmlEscape($_child->getName()) ?>"><img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$_child->getThumbnail() ?>" width="580" max-height="300px" alt="<?php echo
        $this->htmlEscape($_child->getName()) ?>" /></a>
        <h1><?php echo
        $this->htmlEscape($_child->getName()) ?></h1>
                    </li>

        <?php } ?>
     </ul>
   </div> 
   <?php } ?>

So now i want to sort them Alphabetically how i can achieve that.

Was it helpful?

Solution

use ksort

$_categories=array();
foreach($catArray as $child)
                  {
                $_child = Mage::getModel( 'catalog/category' )->load( $child ); 


   $_categories[$_child->getName()] = $_child;
}
ksort($_categories);

Now it will sorted alphabetic order

  foreach($_categories as $_child)
                  {

        <li class="item <?= $class ?> sub-cat-items">
        <a href="<?php echo $_child->getURL() ?>" title="<?php echo   $this->
        htmlEscape($_child->getName()) ?>"><img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$_child->getThumbnail() ?>" width="580" max-height="300px" alt="<?php echo
        $this->htmlEscape($_child->getName()) ?>" /></a>
        <h1><?php echo
        $this->htmlEscape($_child->getName()) ?></h1>
                    </li>

        <?php }

OTHER TIPS

For Understand first try this

  <?php
     echo "<pre/>";
    $categories = Mage::getResourceModel('catalog/category_collection')
                  ->addAttributeToSort('name','ASC')
                  ->getData();

    for ($i=0; $i < count($categories); $i++) 
    { 
        echo  $categories[$i]['name']."<br/>";
    }

    die();
    ?>

For Output :-

<?php    $_categories = Mage::getModel('catalog/category')->getCollection()
                     ->addAttributeToSelect('name')
                     ->addAttributeToSelect('is_active')
                     ->addAttributeToSort('name','ASC')
                     ->addUrlRewriteToResult();

 foreach($_categories as $_category)
 {
    echo "<a href=".$_category->getUrl($_category).">".$_category->getName($_category)."</a><br/>";
    //echo "<pre/>";
    //echo $_category->getUrl($_category);
    }
die;
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top