php无限分类实现一二

1 -- 2 -- Table structure for table `category` 3 -- 4 5 CREATE TABLE IF NOT EXISTS `category` ( 6 `id` int(11) NOT NULL AUTO_INCREMENT, 7 `catpath` varchar(255) DEFAULT NULL, 8 `name` varchar(255) DEFAULT NULL, 9 PRIMARY KEY (`id`) 10 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ; 11 12 -- 13 -- Dumping data for table `category` 14 -- 15 16 INSERT INTO `category` (`id`, `catpath`, `name`) VALUES 17 (1, '0', '网站首页'), 18 (2, '0-1', 'Linux OS'), 19 (3, '0-1', 'Apache服务器'), 20 (4, '0-1', 'MySQL数据库'), 21 (5, '0-1', 'PHP脚本语言'), 22 (6, '0-1-2', 'Linux 系统教程'), 23 (7, '0-1-2', 'Linux 网络技术'), 24 (8, '0-1-2', 'Linux 安全基础'), 25 (9, '0-1-2-7', 'Linux LAMP'), 26 (10, '0-1-3', 'apache Server');
1 $conn = mysql_connect ( 'localhost', 'root', '' );
2 mysql_select_db ( 'test', $conn );
3 mysql_query ( 'set names UTF8' );
4 $sql = "select id,concat(catpath,'-',id) as abspath,name from category order by abspath";
5 $query = mysql_query ( $sql );
6 while ( $row = mysql_fetch_array ( $query ) ) {
7 /**
8 * 第一种展示方法
9 */
10 /*$space = str_repeat ( ' ', count ( explode ( '-', $row ['abspath'] ) ) - 1 );
11 echo $space . $row ['name'] . '<br>';*/
12 /**
13 * 第二种展示方法
14 */
15 $space = str_repeat ( ' ', count ( explode ( '-', $row ['abspath'] ) ) - 1 );
16 $option .= '<option value="' . $row ['id'] . '">' . $space . $row ['name'] . '</option>';
17 }
18 echo '<select name="opt">' . $option . '</select>';

转自:http://www.cnblogs.com/BraveCheng/archive/2011/08/26/brave.html
浙公网安备 33010602011771号