<?php CREATE TABLE `menu` ( `id` int(11) NOT NULL auto_increment, `label` varchar(50) NOT NULL default '', `link` varchar(100) NOT NULL default '#', `parent` int(11) NOT NULL default '0', `sort` int(11) default NULL, PRIMARY KEY (`id`)) ------------------------------------------------------------------------------------------------------ $mysql=mysql_connect('127.0.0.1','root',''); mysql_select_db('test',$mysql); function display_menu($parent, $level) { $result = mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent); echo "<ul>"; while ($row = mysql_fetch_assoc($result)) { if ($row['Count'] > 0) { echo "...