php无限极分类

  数据

-- ----------------------------
-- Table structure for `category`
-- ----------------------------
DROP TABLE IF EXISTS `category`;
CREATE TABLE `category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pid` int(11) DEFAULT NULL,
  `catename` varchar(100) DEFAULT NULL,
  `cateorder` int(11) DEFAULT NULL,
  `addtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of category
-- ----------------------------
INSERT INTO `category` VALUES ('1', '0', '新闻', null, '2016-06-16 08:44:57');
INSERT INTO `category` VALUES ('2', '1', '国内新闻', null, '2016-06-16 08:45:04');
INSERT INTO `category` VALUES ('3', '1', '国际新闻', null, '2016-06-16 08:45:06');
INSERT INTO `category` VALUES ('4', '2', '北京新闻', null, '2016-06-16 08:45:52');
INSERT INTO `category` VALUES ('5', '2', '青岛新闻', null, '2016-06-16 08:45:52');
INSERT INTO `category` VALUES ('6', '3', '美国新闻', null, '2016-06-16 08:45:52');
INSERT INTO `category` VALUES ('7', '3', '英国新闻', null, '2016-06-16 08:45:52');
INSERT INTO `category` VALUES ('8', '0', '图片', null, '2016-06-16 08:46:16');
INSERT INTO `category` VALUES ('9', '8', '美女图片', null, '2016-06-16 08:48:33');
INSERT INTO `category` VALUES ('10', '8', '动漫图片', null, '2016-06-16 08:48:33');
INSERT INTO `category` VALUES ('11', '9', '韩国美女', null, '2016-06-16 08:49:49');
INSERT INTO `category` VALUES ('12', '9', 'AV美女', null, '2016-06-16 08:49:49');
INSERT INTO `category` VALUES ('13', '10', '海贼王', null, '2016-06-16 08:49:50');
INSERT INTO `category` VALUES ('14', '10', '名侦探柯南', null, '2016-06-16 08:49:50');
INSERT INTO `category` VALUES ('15', '10', '网球王子', null, '2016-06-16 08:49:50');
INSERT INTO `category` VALUES ('16', '12', '苍井空', null, '2016-06-16 08:50:46');
INSERT INTO `category` VALUES ('17', '12', '波多野结衣', null, '2016-06-16 08:50:46');
View Code

  代码

 1 /** 
 2      * @author lhat 
 3      * @todo   PHP无限极分类 
 4      */ 
 5     $cn = mysql_connect('localhost', 'root', 'root') or die(mysql_error()); 
 6     mysql_select_db('test', $cn) or die(mysql_error()); 
 7     mysql_query('set names utf8'); 
 8     
 9     /** 
10      * 从顶层逐级向下获取子类 
11      * @param number $pid 
12      * @param number $deep 
13      * @return array 
14      */ 
15     function getLists($pid = 0, $deep = 0) { 
16         $list = array();
17         $sql = 'SELECT * FROM category WHERE pid='.$pid; 
18         $res = mysql_query($sql); 
19         while(($row = mysql_fetch_assoc($res)) !== FALSE ) { 
20             $row['children'] = getLists($row['id'], ++$deep); //进入子类之前深度+1     
21             $list[]    = $row;        
22             --$deep; //从子类退出之后深度-1     
23         } 
24         return $list; 
25     }
26     function displayList($list, $deep=0){
27         $str = '';
28         foreach($list as $item){
29             $str .= '<option>' . str_repeat('—', $deep) .'|-'. $item['catename'] . '</option>';
30             if(!empty($item['children'])){
31                 $str .= displayList($item['children'], ++$deep);
32                 --$deep;
33             }
34         }
35         return $str;
36     }
37     /** 
38      * 从子类开始逐级向上获取其父类 
39      * @param number $cid 
40      * @param array $category 
41      * @return array: 
42      */ 
43     function getCategory($cid, &$category = array()) { 
44         $sql = 'SELECT * FROM category WHERE id='.$cid.' LIMIT 1'; 
45         $result = mysql_query($sql); 
46         $row = mysql_fetch_assoc($result); 
47         if ( $row ) { 
48             $category[] = $row; 
49             getCategory($row['pid'], $category); 
50         }
51         
52         krsort($category); //逆序,达到从父类到子类的效果     
53         return $category; 
54     } 
55     function displayCategory($cid) { 
56         $result = getCategory($cid); 
57         $str = ""; 
58         foreach($result as $item ) { 
59             $str .= '<a href="'.$item['id'].'">'.$item['catename'].'</a>>'; 
60         } 
61         return substr($str, 0, strlen($str) - 1); 
62     } 
63     echo '<select>' . displayList(getLists()) . '</select>';
64     
65     echo displayCategory(13);

效果

原文地址:https://www.cnblogs.com/lhat/p/5590083.html