<?php
?>
<?php
 
class Lehui_AllCategoryList_Block_List extends Mage_Core_Block_Template
{
    
    protected $_store;
    protected $_baseUrl;
    
    protected function _construct(){
        parent::_construct();
        $this->_init();
    }
    
    protected function _init(){
        $this->_store=Mage::app()->getStore();
         $this->_baseUrl=Mage::getBaseUrl();
    }
    
    
    public function getAllCategory(){
        
        $parentId = 1;
        
        $tree = Mage::getResourceSingleton('catalog/category_tree')->load();
        $root = $tree->getNodeById($parentId);
        if($root && $root->getId() == 1) {
            $root->setName(Mage::helper('catalog')->__('Root'));
        }
        foreach(get_class_methods($tree) as $item){
           // echo $item,"</br>";
        }
        $collection = Mage::getModel('catalog/category')->getCollection()
                        ->addAttributeToSelect('name')
                        ->addAttributeToSelect('url_path')
                        ->addAttributeToSelect('path')
                        ->addAttributeToSelect('is_active')
                        ->addIsActiveFilter();
        //echo $collection->getSelectSql();//->addPathFilter('1\\/'.$this->_store->getRootCategoryId().'\\/')
        foreach(get_class_methods($collection) as $item){
            //echo $item,"</br>";
        }
        $tree->addCollectionData($collection, true);
        $root=$this->_nodeToArray($root);
        $this->_print_tree($root['children'],0);
    }
    
    
    protected function _print_tree($tree,$level){
        $level++;
        foreach($tree as $item){
            if($level>1&preg_match('/1\\/'.$this->_store->getRootCategoryId().'\\//',$item['path'])){
                echo str_repeat('&nbsp;&nbsp;', $level).'<a href="'.$item['url'].'">'.$item['name']."</a><br>";
            }
            $this->_print_tree($item['children'],$level); 
        }
    }
    
    
    protected function _nodeToArray(Varien_Data_Tree_Node $node){
        $result = array();
        $result['category_id'] = $node->getId();
        $result['parent_id'] = $node->getParentId();
        $result['name'] = $node->getName();
        $result['is_active'] = $node->getIsActive();
        $result['position'] = $node->getPosition();
        $result['level'] = $node->getLevel();
        $result['url']=$this->_baseUrl.$node->getData('url_path');
        $result['path']=$node->getData('path');
        $result['children'] = array();
        
        foreach ($node->getChildren() as $child) {
            $result['children'][] = $this->_nodeToArray($child);
        }
        
        return $result;
    }
    
    
    
}

magento 得到树形结构的分类列表的更多相关文章

  1. c# List列表数据转换成树形结构

    把List列表结构 转换成树形结构 /// <summary> /// 构造树形Json /// </summary> public static class TreeJson ...

  2. Delphi中根据分类数据生成树形结构的最优方法

    一. 引言:    TreeView控件适合于表示具有多层次关系的数据.它以简洁的界面,表现形式清晰.形象,操作简单而深受用户喜爱.而且用它可以实现ListView.ListBox所无法实现的很多功能 ...

  3. 递归、嵌套for循环、map集合方式实现树形结构菜单列表查询

    有时候, 我们需要用到菜单列表,但是怎么样去实现一个菜单列表的编写呢,这是一重要的问题. 比如我们需要编写一个树形结构的菜单,那么我们可以使用JQuery的zTree插件:http://www.tre ...

  4. SQL 实现地区的实现树形结构递归查询(无限级分类),level为节点层级,由小至大依次

    //SQL 实现地区的实现树形结构递归查询(无限级分类),level为节点层级,由小至大依次 2018-09-25 StringBuilder areaSQL = new StringBuilder( ...

  5. java不需要递归列表转树形结构

    有时候我们需要将列表结构的数据转成树形结构的数据 废话不多说直接上代码 基础类 `@Data public class TreeNode { private Long id; private Long ...

  6. js文章列表的树形结构输出

    文章表设计成这样了 后端直接给了无任何处理的json数据,现在要前端实现树形结构的输出,其实后端处理更简单写,不过既然来了就码出来 var doclist = [{ "id": 1 ...

  7. 【WPF】树形结构TreeView的用法(MVVM)

    TreeView控件的用法还是有蛮多坑点的,最好记录一下. 参考项目: https://www.codeproject.com/Articles/26288/Simplifying-the-WPF-T ...

  8. [从产品角度学EXCEL 02]-EXCEL里的树形结构

    这是<从产品角度学EXCEL>系列第三篇. 前言请看: 0 为什么要关注EXCEL的本质 1 excel是怎样运作的 或者你可以去微信公众号@尾巴说数 获得连载目录. 本文仅由尾巴本人发布 ...

  9. magereverse - Magento数据库表结构

    Magento数据库表结构相当复杂,250多张表包含了非常多的表关联关系,让刚刚接触Magento的开发者来说真的非常头疼.往往是看到一个产品的各种属性分散在非常多的表中,找不到任何办法来取出它们的数 ...

随机推荐

  1. 从Linux服务器下载网站文件

    最近公司迁来一个新客户,该客户的网站是别的网络服务商做的,放在linux主机上,因为客户跟之前的网络服务商合作的不愉快 所以就把网站迁到我们公司,经理让我把网站文件和数据库download下来并在我们 ...

  2. Docker-machine创建虚机时停在虚机启动的提示上,并且创建的虚机显示Ip Not found

    Docker-machine创建虚机时停在虚机启动的提示上,并且创建的虚机用docker-machine ls 列出来的时候显示Ip Not found, 是什么原因那? [答案] 看这个帖子: ht ...

  3. 阿里云centos配置nginx和nodejs

    今天新买了阿里云,想把网站跑起来,于是记录跑起来的过程 1.购买域名 2.购买解析 3.购买ecs主机 4.ssh登录主机 5.安装vsftpd 6.配置ftp用户.文件夹.权限 7.安装nginx/ ...

  4. 快讯:微软安卓版个人助理(Cortana)在美国境内进行公測

    8月24日,"Microsoft starts public test of Cortana app for Android smartphones"(此文8月24日发表),此事意 ...

  5. Mysql数据库常用分库和分表方式

    http://blog.csdn.net/clevercode/article/details/50877580 1 分库 1.1 按照功能分库 按照功能进行分库.常见的分成6大库:    1 用户类 ...

  6. Spring开发 - 通过实现ApplicationContextAware在Servlet中调用注解的Service

    用过Spring MVC的人都知道,我们如何在Controller中注入Service,可以使用@Resource注解的方法. 有时候,实际在项目的过程中,我们需要在某个Servlet中使用Servi ...

  7. SpeechLib 应用

    //引用组件:Interop.SpeechLib.dll //导入空间:SpeechLib //引用组件:Interop.SpeechLib.dll//导入空间:SpeechLib //1.SpVoi ...

  8. asp.net时间类-格式-方法应用

    一.当前日期+时间DateTime.Now c#/asp.net通过DateTime.Now这个类来获取当前的时间. DateTime dt = DateTime.Now; 2013/10/24 10 ...

  9. 分享一个CSS+JavaScript框架materializecss

    一.内容: CSS+JavaScript框架materializecss. 二.网址:http://materializecss.com 三.图片:

  10. Linux内核配置:定制配置选项

    很多嵌入式开发人员都需要在Linux内核中添加一些特性,以支持特别的定制硬件. ARM架构的顶层Kconfig文件中,可以看到一个名为System Type的菜单项.在ARM system type提 ...