Home  >  Article  >  php教程  >  php无限级分类实例

php无限级分类实例

WBOY
WBOYOriginal
2016-05-25 16:43:211694browse

无限分灰一般都会用到递归来实现,下面我们来看看我提供的三个无限分类的读出方法,数据库代码如下:

--  
-- 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 );  
echo $space . $row [&#39;name&#39;] . &#39;<br>&#39;;*/  
/**  
* 第二种展示方法  
*/  
$space = str_repeat ( &#39; &#39;, count ( explode ( &#39;-&#39;, $row [&#39;abspath&#39;] ) ) - 1 );  
$option .= &#39;<option value="&#39; . $row [&#39;id&#39;] . &#39;">&#39; . $space . $row [&#39;name&#39;] . &#39;</option>&#39;;  
}  
echo &#39;<select name="opt">&#39; . $option . &#39;</select>&#39;;


永久地址:

转载随意~请带上教程地址吧^^

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn