Jump to content

Dynamic Dropdown Menu


phpbeginner

Recommended Posts

I have been trying to build a drop down menu with php and data from MySQL database but cannot seem to get it to work. I have the horizontal (main categories) displaying but the drop downs are not working. Appreciate any help and thanks in advance.

<ul id="menu">
<?php
$clsnm="active";
if (isset($_GET["id"])) $clsnm="footerlink";
if (strstr($_SERVER['SCRIPT_NAME'],'events.php')) $clsnm="footerlink";
if (strstr($_SERVER['SCRIPT_NAME'],'gallery.php')) $clsnm="footerlink";
if (strstr($_SERVER['SCRIPT_NAME'],'news.php')) $clsnm="footerlink";
?>
<li class="<?php echo $clsnm;?>"><a href="index.php">Home</a></li>
<li class="seplie"></li>
<?php
$sql = "SELECT id,title,links FROM tblmain where id > 1 and head='Yes' order by sorder asc";
$reccount=$sq->numsrow($sql);
if ($reccount > 0) {
$ata =$sq->query($sql);
while($rs=$sq->fetch($ata))
{
$clsnm="footerlink";
$phppgname=$rs["links"];
if ((isset($_GET["id"])) && ($_GET["id"]==$rs["id"])) $clsnm="active";
if ($phppgname!="") {
if (strstr($_SERVER['SCRIPT_NAME'],$phppgname)) $clsnm="active";
}
if ($rs["links"]=="") $phppgname="contents.php?id=".$rs["id"];
?>
<li class="<?php echo $clsnm;?>"><a href="<?php echo $phppgname;?>"><?php echo $rs["title"];?></a> </li>  
<li class="seplie"></li>
<?php
$pn++;
}
}
?>

<?php  
$pnds=1;
if (isset($_GET["id"])) $pnds=$_GET["id"];
$sql = "SELECT id,title,subpageid FROM tblsubpage where subpageid=".$pnds." order by id desc ";
$reccount=$sq->numsrow($sql);
if ($reccount > 0) {
$ata =$sq->query($sql);
while($rs=$sq->fetch($ata))
{
?>
<ul><li><a href="subcontents.php?id=<?php echo $rs["subpageid"];?>&sid=<?php echo $rs["id"];?>"><?php echo $rs["title"];?></a></li> 
<?php
}
}
?></ul>
</ul>

Link to comment
Share on other sites

no errors.......just blank. If I add the sub navigation (which is what I am trying to use in the dropdown) to the left hand column, it displays fine.

<?php 
$pnds=1;
if (isset($_GET["id"])) $pnds=$_GET["id"];
$sql = "SELECT id,title,subpageid FROM tblsubpage where subpageid=".$pnds." order by id desc ";
$reccount=$sq->numsrow($sql);
if ($reccount > 0) {
$ata =$sq->query($sql);
while($rs=$sq->fetch($ata))
{
?>
<ul><li><a href="subcontents.php?id=<?php echo $rs["subpageid"];?>&sid=<?php echo $rs["id"];?>"><?php echo $rs["title"];?></a></li>
<?php
}
}
?></ul>

Link to comment
Share on other sites

It's a navigation menu. Right now I have this and it works until it gets to first drop down menu....... (ie: shows Home, About Us, Services) under services it drops down as it should to show sub-categories. Then it stops and does not display any more Main Menu items after Services.

<ul id="menu">
<?php
$clsnm="active";
if (isset($_GET["id"])) $clsnm="footerlink";
if (strstr($_SERVER['SCRIPT_NAME'],'events.php')) $clsnm="footerlink";
if (strstr($_SERVER['SCRIPT_NAME'],'gallery.php')) $clsnm="footerlink";
if (strstr($_SERVER['SCRIPT_NAME'],'news.php')) $clsnm="footerlink";
?>
<li class="<?php echo $clsnm;?>"><a href="index.php">Home</a></li>
<li class="seplie"></li>
<?php
$sql = "SELECT id,title,links FROM tblmain where id > 1 and head='Yes' order by sorder asc";
$reccount=$sq->numsrow($sql);
if ($reccount > 0) {
$ata =$sq->query($sql);
while($rs=$sq->fetch($ata))
{
$clsnm="footerlink";
$phppgname=$rs["links"];
if ((isset($_GET["id"])) && ($_GET["id"]==$rs["id"])) $clsnm="active";
if ($phppgname!="") {
if (strstr($_SERVER['SCRIPT_NAME'],$phppgname)) $clsnm="active";
}
if ($rs["links"]=="") $phppgname="contents.php?id=".$rs["id"];
?>
<li class="<?php echo $clsnm;?>"><a href="<?php echo $phppgname;?>"><?php echo $rs["title"];?></a>
<?php  
$pnds=1;
if (isset($_GET["id"])) $pnds=$_GET["id"];
$sql = "SELECT id,title,subpageid FROM tblsubpage where subpageid=".$rs["id"]." order by id desc ";
$reccount=$sq->numsrow($sql);
if ($reccount > 0){
echo("<ul>");
?>
<?php
$ata =$sq->query($sql);
while($rs=$sq->fetch($ata))
{
?>
<li><a href="subcontents.php?id=<?php echo $rs["subpageid"];?>&sid=<?php echo $rs["id"];?>"><?php echo $rs["title"];?></a></li>
<?php
}
echo("</ul>");
}
?> 
<li class="seplie"></li>
<?php
$pn++;
}
}
?>
</ul>

Link to comment
Share on other sites

Barand, by drop down menu he means when you hover ( or click on some sites o.O ) then a sub menu appears. Like on Facebook when you click the settings button at the top of your profile, it then displays a drop down menu with more settings to view, personally however I have always found drop down menus a problem.

 

Try this google search and you might find what you are looking for:

 

http://www.google.co...iw=1280&bih=933

 

Note: Most drop down menus are not supported by some browsers (namely: IE) so it might be worth looking into something that is supported by all browsers. The google link is for pure css menus, if you something with a bit os style to it, you might find jQuery menus better, again though, these are not supported in all browsers.

Edited by White_Lily
Link to comment
Share on other sites

White_Lily, the issue is not creating the drop down. I have tested this one (jQuery) and it works fine in all browsers using standard coding techniques. I am having trouble with the PHP/MySQL coding I am using. I can create a standard horizontal menu and even get it working til the first drop down menu, once it gets to the first drop down menu and it actually works, it stops display the other main menu items beyond that in the horizontal display. As an example, in my test menu I have Home, About Us, Services, and Contact. I have 2 drop down items on services and once it gets there, then Contact menu item does not display. I am doing something wrong in my code below.

///Where main menu starts/// <ul id="menu">
<?php
$clsnm="active";
if (isset($_GET["id"])) $clsnm="footerlink";
if (strstr($_SERVER['SCRIPT_NAME'],'events.php')) $clsnm="footerlink";
if (strstr($_SERVER['SCRIPT_NAME'],'gallery.php')) $clsnm="footerlink";
if (strstr($_SERVER['SCRIPT_NAME'],'news.php')) $clsnm="footerlink";
?>
<li class="<?php echo $clsnm;?>"><a href="index.php">Home</a></li>
<li class="seplie"></li>
<?php
$sql = "SELECT id,title,links FROM tblmain where id > 1 and head='Yes' order by sorder asc";
$reccount=$sq->numsrow($sql);
if ($reccount > 0) {
$ata =$sq->query($sql);
while($rs=$sq->fetch($ata))
{
$clsnm="footerlink";
$phppgname=$rs["links"];
if ((isset($_GET["id"])) && ($_GET["id"]==$rs["id"])) $clsnm="active";
if ($phppgname!="") {
if (strstr($_SERVER['SCRIPT_NAME'],$phppgname)) $clsnm="active";
}
if ($rs["links"]=="") $phppgname="contents.php?id=".$rs["id"];
?>
/// Main menu items/// <li class='<?php echo $clsnm;?>'><a href='<?php echo $phppgname;?>'><?php echo $rs['title'];?></a>
///Sub Menu Items Start///<? php
$pnds=1;
if (isset($_GET["id"])) $pnds=$_GET["id"];
$sql = "SELECT id,title,subpageid FROM tblsubpage where subpageid=".$rs["id"]." order by id desc ";
$reccount=$sq->numsrow($sql);
if ($reccount > 0){
echo("<ul>");
$ata =$sq->query($sql);
while($rs=$sq->fetch($ata))
{
?>
///Sub Menu Items Display/// <li><a href="subcontents.php?id=<?php echo $rs["subpageid"];?>&sid=<?php echo $rs["id"];?>"><?php echo $rs["title"];?></a></li>
<?php
}
echo("</ul>");
}
echo("<li class='seplie'></li>");
///Sub Menu Ends/// $ pn++;
}
}
?> ///Main Menu Ends
</ul>

Edited by phpbeginner
Link to comment
Share on other sites

Corrected issue, same problem............

<ul id="menu">
<?php
$clsnm="active";
if (isset($_GET["id"])) $clsnm="footerlink";
if (strstr($_SERVER['SCRIPT_NAME'],'events.php')) $clsnm="footerlink";
if (strstr($_SERVER['SCRIPT_NAME'],'gallery.php')) $clsnm="footerlink";
if (strstr($_SERVER['SCRIPT_NAME'],'news.php')) $clsnm="footerlink";
?>
<li class="<?php echo $clsnm;?>"><a href="index.php">Home</a></li>
<li class="seplie"></li>
<?php
$sql = "SELECT id,title,links FROM tblmain where id > 1 and head='Yes' order by sorder asc";
$reccount=$sq->numsrow($sql);
if ($reccount > 0) {
$ata =$sq->query($sql);
while($rs=$sq->fetch($ata))
{
$clsnm="footerlink";
$phppgname=$rs["links"];
if ((isset($_GET["id"])) && ($_GET["id"]==$rs["id"])) $clsnm="active";
if ($phppgname!="") {
if (strstr($_SERVER['SCRIPT_NAME'],$phppgname)) $clsnm="active";
}
if ($rs["links"]=="") $phppgname="contents.php?id=".$rs["id"];
?>
<li class='<?php echo $clsnm;?>'><a href='<?php echo $phppgname;?>'><?php echo $rs['title'];?></a>
<?php
if (isset($_GET["id"]));
$sql = "SELECT id,title,subpageid FROM tblsubpage where subpageid=".$rs["id"]." order by id desc ";
$reccount=$sq->numsrow($sql);
if ($reccount > 0){
echo("<ul>");
$ata =$sq->query($sql);
while($rs=$sq->fetch($ata))
{
?>
<li><a href="subcontents.php?id=<?php echo $rs["subpageid"];?>&sid=<?php echo $rs["id"];?>"><?php echo $rs["title"];?></a></li>
<?php
}
echo("</ul>");
}
echo("<li class='seplie'></li>");
$pn++;
}
}
?>
</ul>

Link to comment
Share on other sites

anyone know what I can do with the code to get the remainder of navigation items to display after the first drop down? Something in my code is hitting the first sub-navigation menu item, then not getting the remainder of the main menu items.......script ends after first Main Menu item with a drop down.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.