建立数据库 :
id,fid,fname(id和fid都必需是数字类型而且fid的默认值一定要设成0;
$stime=microtime();
$db=@mysql_connect("localhost","root","micronsky.net") or die("数据库连接失入");
mysql_select_db("temp",$db);
//定义第一级分类
function mainfl()
{
global $db;
$result=mysql_query("select id,fid,fname from wxjfl where fid=0 order by id desc",$db);
if ($myrow=mysql_fetch_array($result)) {
do {
echo $myrow["fname"];
echo "
";
echo subfl($myrow["id"]," "); //此处调用子级分类函数
}
while ($myrow=mysql_fetch_array($result));
}
}
//定义子级分类
function subfl($fid,$strdis)
{
global $db;
$result1=mysql_query("select id,fid,fname from wxjfl where fid=$fid order by id desc",$db);
if ($myrow1=mysql_fetch_array($result1)) {
do {
//echo $strdis;
echo $strdis.$myrow1["fname"];
echo "
";
subfl($myrow1["id"]," ".$strdis); //这里一定要注意并不需要像上面那个函数那样echo ...只要直接调用子级分类函数并付值就行了!同时这里也是递归部分
}
while ($myrow1=mysql_fetch_array($result1));
}
}
echo mainfl();
$ltime=microtime();
echo "
";
echo number_format($ltime-$stime,4); //统计执行时间,这里就比ASP快多了,但这跟写法没多大关系,主要是PHP本身加入了加速器的原因!
?>
阅读(2390) | 评论(0) | 转发(0) |