<?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. 红米1线刷救砖教程V5版(移动联通适用,线刷包永久有效)

    红米1线刷救砖教程V5版(移动联通适用,线刷包永久有效) 原文来自:http://www.miui.com/thread-1890972-1-1.html?mobile=2 ,加了些自己的经验. (我 ...

  2. [Jest] Set up Testing Globals in an Application with Jest

    For some React component testing, we have common setup in each test file: import { render } from 're ...

  3. [Git] Undo a commit that has already been pushed to the remote repository

    If we pushed our changes already to the remote repository we have to pay attention to not change the ...

  4. [Canvas]用透明PNG图在背景上画前景能不遮挡背景

    欲看动态效果请点击下载并用Chrome/Firefox浏览器打开index,html. 图例: 从效果可以明显的看到,五角星边缘和中心都没有对背景遮挡. 代码: <!DOCTYPE html&g ...

  5. SliTaz 5.0 截图

  6. Android百日程序:绘画程序-画手指路径

    本程序实如今一个画布中,用手指绘图的效果. 须要使用的知识: 1 Canvas 画布,动态保存更新当前画面 2 Path 记录并画出手接触屏幕经过的路径 如以下效果图: 仅仅须要依照默认设置新建一个项 ...

  7. VMware用于Site Recovery Manager 5的vSphere Replication功能一览

    http://www.searchstorage.com.cn/showcontent_54838.htm 参考:深度解析SRM 5.0和vSphere Replication http://wenk ...

  8. C# FTP常规方法

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  9. Python 模块浅析

    如果你退出Python解释器重新进入,以前创建的一切定义(变量和函数)就全部丢失了.因此,如果你想写一些长久保存的程序,最好使用一个文本编辑器来编写程序,把保存好的文件输入解释器. 我们称之为创建一个 ...

  10. Storm本地模式异常

    来自:http://isuifengfei.iteye.com/blog/1998265   问题1 java.net.SocketException: Address family not supp ...