OUTPUT:
";
PHP Code:
error_reporting(0);
$con=mysql_connect("localhost","root","");
mysql_select_db("megamenu",$con);
function display_children($parent, $level) {
$result = mysql_query("SELECT a.id, a.label, a.link, veer.Count FROM `menu` a LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) veer ON a.id = veer.parent WHERE a.parent=" . $parent);
echo "
- ";
while ($row = mysql_fetch_assoc($result)) {
if ($row['Count'] > 0) {
echo "
" . $row['label'] . "";
display_children($row['id'], $level + 1);
echo "
} elseif ($row['Count']==0) {
echo "
" . $row['label'] . "
";
} else;
}
echo "
";
}
display_children(0, 1);
?>
MYSQL Query:
--
-- Database: `megamenu`
--
-- --------------------------------------------------------
--
-- Table structure for table `menu`
--
CREATE TABLE IF NOT EXISTS `menu` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`label` varchar(50) NOT NULL,
`link` varchar(50) NOT NULL DEFAULT '#',
`parent` int(11) NOT NULL DEFAULT '0',
`sort` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
--
-- Dumping data for table `menu`
--
INSERT INTO `menu` (`id`, `label`, `link`, `parent`, `sort`) VALUES
(1, 'Home', '#', 0, 0),
(2, 'Code', '#', 0, 0),
(3, 'C', '#', 2, 0),
(4, 'C++', '#', 2, 0),
(5, 'Contact', '#', 0, 0),
(6, 'Scripts', '#', 4, 0),
(7, 'Help', '#', 4, 0),
(8, 'Archive', '#', 6, 0),
(9, 'Snippet', '#', 8, 0);
No comments:
Post a Comment
Dharamart.blogspot.in