我尝试这段代码,它的工作原理..您将尝试一下…
CREATE TABLE IF NOT EXISTS `categorylist` ( `id` int(5) NOT NULL auto_increment,`cname` varchar(25) collate utf8_unipre_ci default NULL,`pid` int(5) NOT NULL,PRIMARY KEY (`id`),KEY `pid` (`pid`)) ;INSERT INTO `categorylist` (`id`, `cname`, `pid`) VALUES(1, 'Entertainment', 0),(2, 'movies', 1),(3, 'actor', 2),(4, 'actress', 2),(5, 'Drama', 1),(7, 'sports', 0),(8, 'comedian', 2),(9, 'political', 0);<?phpinclude "header.php"; include "dbconn.php"; $qry="SELECt * FROM categorylist"; $result=mysql_query($qry); $arrayCategories = array(); while($row = mysql_fetch_assoc($result)){ $arrayCategories[$row['id']] = array("pid" => $row['pid'], "name" => $row['cname']); }//createTree($arrayCategories, 0); function createTree($array, $currentParent, $currLevel = 0, $prevLevel = -1) {foreach ($array as $categoryId => $category) { if ($currentParent == $category['pid']) { if ($currLevel > $prevLevel) echo " <ul> "; if ($currLevel == $prevLevel) echo " </li> "; echo '<li id="'.$categoryId.'" onclick=child(this.id);><span>'.$category['name'].'</span>'; if ($currLevel > $prevLevel) { $prevLevel = $currLevel; } $currLevel++; createTree ($array, $categoryId, $currLevel, $prevLevel); $currLevel--; }}if ($currLevel == $prevLevel) echo " </li> </ul> ";} ?><div id="content" ><?php if(mysql_num_rows($result)!=0) {?><ul> <li id="0" ><span>Categories</span> <?php createTree($arrayCategories, 0); ?></li></ul><?php}?></div>


