php无限分类使用concat如何实现
一、数据库设计
-- --Tablestructurefortable`category` -- CREATETABLE`category`( `id`int(11)NOTNULLauto_increment, `catpath`varchar(255)defaultNULL, `name`varchar(255)defaultNULL, PRIMARYKEY(`id`) )ENGINE=MyISAMDEFAULTCHARSET=utf8AUTO_INCREMENT=11; -- --Dumpingdatafortable`category` -- INSERTINTO`category`VALUES(1,'0','网站首页'); INSERTINTO`category`VALUES(2,'0-1','LinuxOS'); INSERTINTO`category`VALUES(3,'0-1','Apache服务器'); INSERTINTO`category`VALUES(4,'0-1','MySQL数据库'); INSERTINTO`category`VALUES(5,'0-1','PHP脚本语言'); INSERTINTO`category`VALUES(6,'0-1-2','Linux系统教程'); INSERTINTO`category`VALUES(7,'0-1-2','Linux网络技术'); INSERTINTO`category`VALUES(8,'0-1-2','Linux安全基础'); INSERTINTO`category`VALUES(9,'0-1-2-7','LinuxLAMP'); INSERTINTO`category`VALUES(10,'0-1-3-10','apacheServer');
这里说明下,catpath的-链接符号不是固定的,可以选择,;等特殊符号。
二、PHP代码实现
<?
$conn=mysql_connect('localhost','root','root');
mysql_select_db('test',$conn);
mysql_query('setnamesUTF8');
$sql="selectid,concat(catpath,'-',id)asabspath,namefromcategoryorderbyabspath";
$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.='<optionvalue="'.$row['id'].'">'.$space.$row['name'].'</option>';
}
echo'<selectname="opt">'.$option.'</select>';
?>
MySQLconcat函数可以连接一个或者多个字符串
selectconcat('颜','培','攀')
select`id`,`name`,concat(`id`,'-',`name`)asiname
以上就是本文的全部内容,介绍了php使用concat实现无线分类,希望对大家的学习有所帮助。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下