您好,欢迎访问一九零五行业门户网

PHP 运用concat的无限分类

php 使用concat的无限分类
?
?
一、数据库设计:
?
?
-- -- table structure for table `category`-- create table `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` values (1, '0', '网站首页');insert into `category` values (2, '0-1', 'linux os');insert into `category` values (3, '0-1', 'apache服务器');insert into `category` values (4, '0-1', 'mysql数据库');insert into `category` values (5, '0-1', 'php脚本语言');insert into `category` values (6, '0-1-2', 'linux 系统教程');insert into `category` values (7, '0-1-2', 'linux 网络技术');insert into `category` values (8, '0-1-2', 'linux 安全基础');insert into `category` values (9, '0-1-2-7', 'linux lamp');insert into `category` values (10, '0-1-3-10', 'apache server');
?
?
这里说明下,catpath的-链接符号不是固定的,可以选择,;等特殊符号。
二、 php代码实现:
?
?
$conn = mysql_connect ( 'localhost', 'root', '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 ['name'] . '
';*/ //第二种展示方法 $space = str_repeat ( '    ', count ( explode ( '-', $row ['abspath'] ) ) - 1 ); $option .= '' . $space . $row ['name'] . '';}echo '' . $option . '';?>
?
mysql concat函数可以连接一个或者多个字符串
select concat('颜','培','攀')
select `id`,`name`,concat(`id`,'-',`name`) as iname
?
?
?
关联:
简单的无限分级类 category, menus, jquery tree view??
?
?
?
?
?
其它类似信息

推荐信息