无限极分类

mysql> use admin
Database changed
mysql> create table cate(
    -> id int unsigned not null primary key auto_increment,
    -> name char(15) not null,
    -> pid int unsigned not null,
    -> sort smallint(6) not null);
Query OK, 0 rows affected (0.48 sec)

mysql> desc cate;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | char(15)         | NO   |     | NULL    |                |
| pid   | int(10) unsigned | NO   |     | NULL    |                |
| sort  | smallint(6)      | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+

  

public function index(){
            import('common.category',COMMON_PATH);
            $cate=M('cate')->order('sort ASC')->select();
            $catea=new \category();
            $catex=$catea::zifenleia($cate);

            print_r($catex);
//            $this->assign('catex',$catex);
//            $this->display();
        }

        public  function addcate(){
//            $pid=isset($_GET['pid'])?$_GET['pid']:0;
            $this->pid=I('pid',0,'intval');
            $this->display();
        }

        public  function ADDreturn(){
            if(M('cate')->add($_POST)){
                $this->success('添加成功',U(MODULE_NAME.'/Category/index'));
            }else{
                $this->error('出错');
            }
        }
        //排序
        public function sort(){
            $db=M('cate');
           foreach($_POST as $id=>$sort){
               $db->where(array('id'=>$id,))->setField(array('sort'=>$sort));
           }
            $this->redirect(MODULE_NAME.'/category/index');

        }
        //删除
        public function delete(){
            $db=M('cate');

        }

}
class Category{
            /**
             * 组合一维数组,用于输出顶级栏目所属的二级栏目
             */
            Static Public function unlimitedForLevel($m, $html='--', $f_id=0, $level=0){
                $arr=array();
                foreach ($m as $v){
                    if ($v['f_id'] == $f_id){
                        $v['level']=$level+1;
                        $v['html']=str_repeat($html,$level);
                        $arr[]=$v;
                        $arr=array_merge($arr,self::unlimitedForLevel($m,$html,$v['id'],$level+1));
                    }
                }
                
                return $arr;
            }
            
            /**
             * 多维数组,用于在前台输出含有二级栏目的导航条
             */
            Static Public function unlimitedForLayer ($m, $name = 'child', $f_id = 0) {
                $arr = array();
                foreach ($m as $v) {
                    if ($v['f_id'] == $f_id) {
                        $v[$name] = self::unlimitedForLayer($m, $name, $v['id']);
                        $arr[] = $v;
                    }
                }
                return $arr;
            }
            
            /**
             * 传递一个子分类ID返回所有的父级分类
             */
            Static Public function getParents ($m, $id) {
                $arr = array();
                foreach ($m as $v) {
                    if ($v['id'] == $id) {
                        $arr[] = $v;
                        $arr = array_merge(self::getParents($m, $v['f_id']), $arr);
                    }
                }
                return $arr;
            }
            
            /**
             * 传递一个父级分类ID返回所有子分类
             */
            Static Public function getChilds ($m, $f_id) {
                $arr = array();
                foreach ($m as $v) {
                    if ($v['f_id'] == $f_id) {
                        $arr[] = $v;
                        $arr = array_merge($arr, self::getChilds($m, $v['id']));
                    }
                }
                return $arr;
            }
            

            /**
             * 传递一个父级分类ID返回所有子分类ID(注意:这里的f_id是栏目表的父级id,根据数据库表的名字填写)
             */
            Static Public function getChildsId ($m, $f_id) {
                $arr = array();
                foreach ($m as $v) {
                    if ($v['f_id'] == $f_id) {
                        $arr[] = $v['id'];
                        $arr = array_merge($arr, self::getChildsId($m, $v['id']));
                    }
                }
                return $arr;
            }
            
            
            /**
             * 传递一个子分类ID返回父级分类ID(注意:这里的f_id是栏目表的父级id,根据数据库表的名字填写)
             */
            Static Public function getfId ($m, $id) {
                $arr = array();
                foreach ($m as $v) {
                    if ($v['id'] == $id) {
                        $arr[] = $v['f_id'];
                        $arr = array_merge($arr, self::getfId($m, $v['f_id']));
                    }
                }
                return $arr;
            }
            
            
}
Array
(
    [0] => Array
        (
            [id] => 1
            [name] => PHP
            [pid] => 0
            [sort] => 1
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [name] => PHP1
                            [pid] => 1
                            [sort] => 100
                            [child] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 6
                            [name] => sdasd
                            [pid] => 1
                            [sort] => 100
                            [child] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [name] => MYSQL
            [pid] => 0
            [sort] => 2
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [name] => MYSQL   2
                            [pid] => 2
                            [sort] => 100
                            [child] => Array
                                (
                                )

                        )

                )

        )

    [2] => Array
        (
            [id] => 3
            [name] => LInux
            [pid] => 0
            [sort] => 3
            [child] => Array
                (
                )

        )

)

 

posted @ 2016-01-18 13:45  尘梦  阅读(431)  评论(0编辑  收藏  举报