Thinkphp achieve unlimited classification by mysql

CREATE TABLE IF NOT EXISTS `think_cate` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(30) NOT NULL,
  `pid` int(11) NOT NULL,
  `path` varchar(200) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ;
INSERT INTO `think_cate` (`id`, `name`, `pid`, `path`) VALUES
(1, '新闻', 0, '0'),
(2, '中国新闻', 1, '0-1'),
(3, '美国新闻', 1, '0-1'),
(4, 'Beijing News', 2,' 0-1-2 '), 
( 5' Washington news', 3, '0-1-3'), 
( 6 'Haidian news', 4,' 0- 1-2-4 '), 
( 7' on the news ', 6' 0-1-2-4-6 '), 
( 8' Seventh Street news ', 7' 0-1-2- 4-6-7 '), 
( 9' 9 Building News', 8 '0-1-2-4-6-7-8'), 
( 11, 'sports', 0,' 0 ') ;
        function index(){
            $cate=D('cate');
            
            $list=$cate->field("id,name,pid,path,concat(path,'-',id) as bpath")->order('bpath')->select();
            
            foreach($list as $key=>$value){
                $list[$key]['count']=count(explode('-',$value['bpath']));
            }
            

            $this->assign('alist',$list);
            
            $this->display();
        }
请选择父级栏目<select name="pid" size="20">
            <option value="0">根栏目</option>
            <volist name="alist" id="vo">
            <option value="{$vo['id']}">
                <php>
                    for($i=0;$i<$vo['count']*2;$i++){
                                echo '&nbsp;';
                    
                    }
                </php>
                {$vo['name']}</option>
            </volist>

</select><br>

 

Guess you like

Origin www.cnblogs.com/fksdy/p/11490993.html