一、数据库设计
-- -- 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实现无线分类,希望对大家的学习有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!