Dynamic multi-level css drop down menu with php and mysql support problem

Hi all,

I am tryin to work on a new project a simple multi-level drop down menu using css and php mysql my problem is that i have written this function to get the information i need from dabase and im trying to get the levels on my sub menu to be drop down i am currently trying to follow a tutorial and this is what i have atm

function display_children($parent, $level) {
					include("dbconnect.php");
					$r = "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."";
				$result = mysqli_query($con,$r);
				echo "<ul>";
				while ($row = mysqli_fetch_assoc($result)) {
					if ($row['Count'] > 0) {
						echo "<li class='menu'><a class='list_link' href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
						display_children($row['id'], $level + 1);
						echo "</li>";
					} elseif ($row['Count']==0) {
						echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
					} else;
				}
				echo "</ul>";
			}
			display_children(0, 1);

HTML CODE

<style type="text/css">
			.menu ul{color:#FFF;} /* Main container, includes the background of the static portion of the menu */
				.menu ul li{color:#FFF;} /* This is the style for the main menu items */
.menu ul ul{color:#FFF;} /* This is the container for the first submenu */
.menu ul ul li{color:#FFF;} /* This is the style for the submenus */
			</style>

and the data in database showing this on output

Home
Code
PHP
Scripts
Archive
Snippet
Help
CSS
Contact

however this is the db structure

REATE 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`),
) ENGINE=MyISAM AUTO_INCREMENT=248 DEFAULT CHARSET=latin1;

records in this table is

id	label	link	parent	sort
1	Home	#home	0	0
2	Code	#code	0	0
3	Contact	#contact	0	0
4	PHP	#php	2	0
5	CSS	#css	2	0
6	Scripts	#scripts	4	0
7	Help	#help	4	0
8	Archive	#archive	6	0
9	Snippet	#snippet	8	0

So what am i doing wrong can anyone help?

Thanks,William

try this

http://www.phpclasses.org/package/5927-PHP-Display-an-hierarchic-menu-stored-in-a-MySQL-table.html

i had a look not what i wanted any how i did attempt to this only issue now that remains is that i want all my sub menus to appear where it is shown in db

here is my db structure

CREATE TABLE IF NOT EXISTS pages (
PID int(11) NOT NULL AUTO_INCREMENT,
PName varchar(20) CHARACTER SET utf8 DEFAULT NULL,
PContent text CHARACTER SET utf8,
PSTATUS varchar(12) CHARACTER SET utf8 DEFAULT NULL,
parentid int(2) NOT NULL,
PRIMARY KEY (PID)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=171 ;

Now the problem remains is if i have somthing inserted in my pages table and this is how its ment to show

Home Products and contacts

only issue is that if i place an item under my products parent id it works fine however i place an another row under a child of the parent id it does not show

If that makes any sense

this is my row insert code for that

(2, ‘Products’, ‘Dsdadas’, ‘PUBLISH’, 0),
(3, ‘DVD’, ‘dvd’, ‘PUBLISH’, 2),
(5, ‘Power Rangers Legacy’, ‘SADASD’, ‘PUBLISH’, 2),
(6, ‘Toys’, ‘asdasd’, ‘PUBLISH’, 0),
(7, ‘Digimon Toys’, ‘hsidadsasd’, ‘PUBLISH’, 3),
(8, ‘Pokemon Toys’, ‘hihih’, ‘PUBLISH’, 7);

As you can see the item row say pokemon toys is ment to have the parent id 6 which is the child of toys only issue is if i change it to say Pokemon toys to be a child of Digimon toys so when i hover over toys and then hover over digimon toys should popdown a new menu with the pokemon toys options

Sorry for silly names im just trying to get my issue fixed

As for my php code here it is below

<style type=“text/css”>
ul#menu,
ul#menu ul
{
list-style-type: none; /* Hide the bullet points */
margin: 0;
padding: 0;
}

ul#menu li
{
	margin: 0;
	border: 0;
	
	float: left;
	width: 8em;
	text-align: center;
	border: 1px solid black;
	position: relative; /* Make any elements inside this (ie. the dropdowns) position relative to it*/
}

ul#menu li a
{
	display: block; /* Make link fill whole list item */
}

ul#menu ul
{
	position: absolute; /* Works with the position: relative on the li. Means "position this relative to the li" */
	left: -1px;
	
	display: none; /* Hide submenus by default */
}

/* Show submenus when hovered */
ul#menu li:hover ul
{
	display: block;
}
&lt;/style&gt;

First PHP Code is this


<ul id="menu" style="text-align:center">
        <?
			 	include("dbconnect.php");
				include("functions.php");
				$uQuery="SELECT * FROM pages WHERE PSTATUS='Publish' AND parentID='0'";
				$rs=mysqli_query($con,$uQuery);
				if(!$rs)
				{
					echo "Error:".mysqli_error($con);
				}
				else
				{
					$count=$rs->num_rows;
					if($count>0)
					{
						while($data=$rs->fetch_assoc())
						{
							$pid = $data['PID'];
							if(CheckChildHasParent($pid) == false)
							{
								echo '<li><a href="index.php?PID="'.$data['PID'].'">'.$data['PName'].'</a></li>';
							}
							else if(CheckChildHasParent($pid) == true)
							{
								if(HasSubChild($pid) == true)
								{
									echo '<li><a href="index.php?PID="'.$data['PID'].'">'.$data['PName'].'</a>';
									echo ShowChildren($pid);
									echo '</li>';
								}
								else
								{
									//
									echo '<li><a href="index.php?PID="'.$data['PID'].'">'.$data['PName'].'</a>';
									echo ShowChildren($pid);
									echo '</li>';
									//	
								}
							}
						}
					}
				}
		?>
        </ul>

The functions for that is this which i wrote and current work for menus of parent ids but does not work for dropdown of children dropdown menus

//
function CheckChildHasParent($pid)
{
include(“dbconnect.php”);
$uQuery=“SELECT * FROM pages WHERE parentid=‘pid’”;
$rs=mysqli_query($con,$uQuery);
if(!$rs)
{
echo “Check Parent Error”.mysqli_error($con);
}
else
{
$count=$rs->num_rows;
if($count>1)
{
return true;
}
else
{
return false;
}
}
}
//
function ShowChildren($parent)
{
include(“dbconnect.php”);
$uQuery=“SELECT * FROM pages WHERE parentid=‘$parent’”;
$rs=mysqli_query($con,$uQuery);
if(!$rs)
{
echo “Show Children Error:”.mysqli_error($con);
}
else
{
$count=$rs->num_rows;
if($count>0)
{
echo ‘<ul>’;
while($data=$rs->fetch_assoc())
{
echo ‘<li><a href=“index.php?PID=”’.$data[‘PID’].‘">’.$data[‘PName’].‘</a></li>’;
}
echo ‘</ul>’;
}
}
}
//
function ShowSubChildren($parent)
{
include(“dbconnect.php”);
$uQuery=“SELECT * FROM pages WHERE parentid=‘$parent’”;
$rs=mysqli_query($con,$uQuery);
if(!$rs)
{
echo “Show Children Error:”.mysqli_error($con);
}
else
{
$count=$rs->num_rows;
if($count>0)
{
echo ‘<ul class=“submenu”>’;
while($data=$rs->fetch_assoc())
{
echo ‘<li><a href=“index.php?PID=”’.$data[‘PID’].‘">’.$data[‘PName’].‘</a></li>’;
}
echo ‘</ul>’;
}
}
}

So How can i get round to this problem? what am i doing wrong am i missing somthing in my css coding?

Thanks,William

i have solve the problem you just follow the link and your problem solve

http://stackoverflow.com/questions/12767863/generation-of-multi-level-menu?rq=1