/**
* php递归实现的无限分类
*
*
*/
header("Content-type: text/html; charset=utf-8");
error_reporting(E_ALL);
//mysql_connect ...
$link = mysql_connect('localhost','root','');
mysql_select_db('test');
mysql_query('set names utf8');
global $i,$parentid;
function tree($parentid) {
$i = 0;
$result = mysql_query('Select * FROM `test_fenlei` Where `parent_id` = '.$parentid) OR die("Invalid query: " . mysql_error());
while($res = mysql_fetch_array($result))
{
echo $i++;
echo str_repeat(' ',$res['level']).$res['name'].'
';
tree($res['id']);
}
}
if(($parentid + 0) < 1){
$parentid = 0;
}
tree(0);
得到结果