当前位置: 移动技术网 > IT编程>开发语言>PHP > php无限分类使用concat如何实现

php无限分类使用concat如何实现

2018年04月29日  | 移动技术网IT编程  | 我要评论
一、数据库设计 -- -- table structure for table `category` -- create table `

一、数据库设计

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

mysql concat函数可以连接一个或者多个字符串

select concat('颜','培','攀')
select `id`,`name`,concat(`id`,'-',`name`) as iname

以上就是本文的全部内容,介绍了php使用concat实现无线分类,希望对大家的学习有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网