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实现无线分类,希望对大家的学习有所帮助。