无限分灰一般都会用到递归来实现,下面我们来看看我提供的三个无限分类的读出方法,数据库代码如下:
- --
- -- Table structure for table `category`
- --
- CREATE TABLE IF NOT EXISTS `category` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `catpath` varchar(255) DEFAULT NULL,
- `name` varchar(255) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;
- --
- -- Dumping data for table `category`
- --
- INSERT INTO `category` (`id`, `catpath`, `name`) VALUES
- (1, '0', '网站首页'),
- (2, '0-1', 'Linux OS'),
- (3, '0-1', 'Apache服务器'),
- (4, '0-1', 'MySQL数据库'),
- (5, '0-1', 'PHP脚本语言'),
- (6, '0-1-2', 'Linux 系统教程'),
- (7, '0-1-2', 'Linux 网络技术'),
- (8, '0-1-2', 'Linux 安全基础'),
- (9, '0-1-2-7', 'Linux LAMP'),
- (10, '0-1-3-10', 'apache Server');
php代码:
- $conn = mysql_connect ( 'localhost', 'root', '' );
- mysql_select_db ( 'test', $conn );
- mysql_query ( 'set names UTF8' );
- $sql = "select id,concat(catpath,'-',id) as abspath,name from category order by abspath";
- $query = mysql_query ( $sql );
- while ( $row = mysql_fetch_array ( $query ) ) {
-
-
-
-
-
-
-
-
- $space = str_repeat ( ' ', count ( explode ( '-', $row ['abspath'] ) ) - 1 );
- $option .= '<option value="' . $row ['id'] . '">' . $space . $row ['name'] . '</option>';
- }
- echo '<select name="opt">' . $option . '</select>';
|