Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts

    Question Horizontal Drop Down Menu & PHP/MySQL

    I've been trying to sort this out for a while and am getting tired so once again... I call on you guys to lend a helping hand.

    Basically, I have a menu on my website that is such as:
    Code:
    <ul>
    <li>Menu Item 1</li>
    <li>Menu Item 2
       <ul>
         <li>Sub Menu 2 - Item 1</li>
         <li>Sub Menu 2 - Item 2</li>
            <ul>
               <li>Sub-Sub Menu 2 - Item 1</li>
               <li>Sub-Sub Menu 2 - Item 2</li>
             </ul>
        </ul>
    </li>
    <li>Menu Item 3</li>
    <li>Menu Item 4
        <ul>
            <li>Sub Menu 4 - Item 1</li>
            <li>Sub Menu 4 - Item 2</li>
        </ul>
    </li>
    </ul>
    Now I have a table in my database which is built like:
    Code:
    #----------------------------------------------------------------------------------------#
    #   id   |   parent   |   text   |          link          |   protected   | sort_order   #
    #----------------------------------------------------------------------------------------#
    #   1    |     0      |   Home   |        /index.php      |      0        |      1       #
    #   2    |     0      |   ByLaws |         /bylaws        |      0        |      2       #
    #   3    |     2      |   Noise  |      /bylaws/noise     |      0        |      3       #
    #   4    |     3      |   Fines   |  /bylaws/noise/fines  |      0        |      4       #
    #----------------------------------------------------------------------------------------#
    I have some PHP code (which is below) that I have for listing out categories and sub items which is recursive but I cannot figure out how to change it around to make up the menu structure I want. :\ I mean anything is possible but after looking at it for so long one just draws blanks.
    PHP Code:
    function display_children($parent$level,$prev=0) {
          global 
    $db;
            
    $result $db->select("SELECT * FROM `table` WHERE parent='".$parent."' ORDER BY `sort_order` ASC");
            if(
    mysql_num_rows($result) > 0) {
                echo 
    '<ul>';
                while (
    $row $db->get_row($result)) {     
                    
    /*for($i = 0 ; $i < $level; $i++) {
                        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                    echo "&nbsp;&nbsp;&nbsp;";*/
                    
    echo '<li>'.$row["text"];
                    
    display_children($row['id'], $level+1,$level);
                    echo 
    '</li>';
                }
                echo 
    "</ul>";
            }
        }
        function 
    getCatDropDown($returnID=0) {
          global 
    $db;
            
    $result $db->select("SELECT * FROM `table` WHERE parent='0' ORDER BY `sort_order` ASC");
            
    //$db->print_last_error();
            
    if(mysql_num_rows($result)) {
                echo 
    '<ul>';           
                while(
    $row=$db->get_row($result)) {   
                    echo 
    "<li>".$row["text"]."";
                       
    display_children($row["id"],0);    
                    echo 
    "</li>\n";
                }
                   echo 
    '</ul>';
            }
        }
        function 
    getCatName($id) {
          global 
    $db;
            
    $getName $db->select("SELECT text FROM `table` WHERE id='".$id."'");
            if(
    mysql_num_rows($getName)) {
                while(
    $row $db->get_row($getName)) {
                    return 
    $row["text"];
                }
            }
        } 
    Last edited by dniwebdesign; 03-06-2013 at 07:12 AM.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    I have some PHP code (which is below) that I have for listing out categories and sub items which is recursive but I cannot figure out how to change it around to make up the menu structure I want.
    You shouldn't execute database queries in recursion/loop.

    Have a look at http://www.codeassembly.com/How-to-d...and-javascript
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    dniwebdesign (03-06-2013)

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    727
    Thanks
    20
    Thanked 85 Times in 85 Posts
    PHP Code:
    //query the database for each level you have
    $result1 $db->select("SELECT * FROM `table` WHERE parent='".$parent."' ORDER BY `sort_order` ASC"); 
    $result2 $db->select("SELECT * FROM `table` ORDER BY `sort_order` ASC"); 

    while (
    $row1 $db->get_row($result1)){
    echo 
    '<li>'.$row["text"]; 
    //etc
    mysql_data_seek($result20); //reset $result2
    while ($row2 $db->get_row($result2)){
    if {
    $row2['parent'] == $row1['ID']){
    echo 
    '<li>'.$row["text"]; 
    //etc
    }
    }


    this is a 3 level menu i use with permissions check:

    PHP Code:
    $Q1 Q("SELECT * FROM  menu WHERE `MenuGroup` = 1");
    if (
    $_SESSION['logged_in'] != true){
        
    $_SESSION['UserID'] = 0;
        
    $menulink SITE.'index.php';
    }else{

    }
    $Q2 Q("SELECT menu.*, user__permissions.*
        FROM  menu LEFT OUTER JOIN user__permissions ON user__permissions.PMenuID = menu.MenuID
        WHERE user__permissions.UserID = "
    .$_SESSION['UserID']." ORDER BY MenuNumber");
    $Q3 Q("SELECT menu.*, user__permissions.*
        FROM  menu LEFT OUTER JOIN user__permissions ON user__permissions.PMenuID = menu.MenuID
        WHERE user__permissions.UserID = "
    .$_SESSION['UserID']);

    $i 0;
    $Menu .= '<ul id="css3menu1" class="topmenu">
    <li class="topfirst"><a class="P0" href="" style="height:18px;line-height:18px;"><span>'
    .iname.'</span></a>
        <ul>
            <li><a class="P1" '
    .$SKIPIT.' href="'.SITE.'logout.php">1 Logout</a></li>
            <li><a class="P1" '
    .$SKIPIT.' href="#">2 User Settings</a></li>
            <li><a class="P1" '
    .$SKIPIT.' href="#">3 Dashboard</a></li>
        </ul>
    </li>
    '
    ;
    while (
    $M1 mysql_fetch_array($Q1)){
        
    $Menu .= '<li class="';
        if (
    $i+== mysql_num_rows($Q1)){$Menu .= 'toplast';$i++;}
        else {
    $Menu .= 'topmenu';$i++;}
        if (
    $M1['Permission'] == 1){
            
    $menulink SITE.$M1['MenuLink'];
    $Menu .= '"><a class="P1" '.$SKIPIT.' href="'.$menulink.'" style="height:18px;line-height:18px;"><span>'.$M1['MenuNumber'].' '.$M1['MenuName'].'</span></a>';
    $menulink '/index.php';
        }else{
    $Menu .= '"><a class="P0" '.$SKIPIT.' href="'.$menulink.'" style="height:18px;line-height:18px;"><span>'.$M1['MenuNumber'].' '.$M1['MenuName'].'</span></a>';
        }
        
    $Sub 0;
        while (
    $M3 mysql_fetch_array($Q3)){
                if (
    $M3['MenuParent'] != null){$Sub++;}
        }
        
    mysql_data_seek($Q30);
        if (
    $Sub == 0){
            
    $Menu .= '</li>
            '
    ;
        }else{
            
    $Sub 0;
            
    $Menu .= '<ul>';
            while (
    $M3 mysql_fetch_array($Q3)){
                if (
    $M3['MenuParent'] == $M1['MenuID']){
                    if (
    $M3['Permission'] == 1){
                        
    $Menu .= '
                    <li><a class="P1" '
    .$SKIPIT.' href="'.SITE.$M3['MenuLink'].'">'.$M3['MenuNumber'].' '.$M3['MenuName'].'</a>';
                    }else{
                    
    $Menu .= '
                    <li><a class="P0" '
    .$SKIPIT.'>'.$M3['MenuNumber'].' '.$M3['MenuName'].'</a>';
                    }
                    
    $Sub 0;
                        while (
    $M2 mysql_fetch_array($Q2)){
                                if (
    $M2['MenuParent'] == $M3['MenuID']){$Sub++;}
                        }
                        
    mysql_data_seek($Q20);
                        if (
    $Sub == 0){
                            
    $Menu .= '</li>
                            '
    ;
                        }else{
                            
    $Sub 0;
                            
    $Menu .= '<ul>';
                            while (
    $M2 mysql_fetch_array($Q2)){
                                if (
    $M2['MenuParent'] == $M3['MenuID']){
                                    if (
    $M2['Permission'] == 1){
                                        if (
    $M2['MenuLink'] != '#'){
                                        
    $Menu .= '
                                    <li><a class="P1" '
    .$SKIPIT.' href="'.SITE.$M2['MenuLink'].'">'.$M2['MenuNumber'].' '.$M2['MenuName'].'-'.$M2['MenuID'].'</a></li>';
                                        }else{
                                        
    $Menu .= '
                                    <li><a class="P2" '
    .$SKIPIT.' href="'.SITE.$M2['MenuLink'].'">'.$M2['MenuNumber'].' '.$M2['MenuName'].'-'.$M2['MenuID'].'</a></li>';    
                                        }
                                    }else{
                                    
    $Menu .= '
                                    <li><a class="P0" '
    .$SKIPIT.'>'.$M2['MenuNumber'].' '.$M2['MenuName'].'-'.$M2['MenuID'].'</a></li>';
                                    }
                                    
    $Sub 0;
                                }
                            }
                            
    mysql_data_seek($Q20);
                            
    $Menu .= '</ul></li>
                            '
    ;
                            }
                }
            }
            
    mysql_data_seek($Q30);
            
    $Menu .= '</ul></li>
            '
    ;
        }
    }
    $Menu .= '</ul><P>'

    and the stylesheet for it

    Code:
    ul#css3menu1,ul#css3menu1 ul{
    	margin:0;list-style:none;padding:0;background-color:#dedede;border-width:1px;border-style:solid;border-color:#5f5f5f;-moz-border-radius:5px;-webkit-border-radius:5px;border-radius:5px;}
    ul#css3menu1 ul{
    	display:none;position:absolute;left:0;top:100%;-moz-box-shadow:3.5px 3.5px 5px #000000;-webkit-box-shadow:3.5px 3.5px 5px #000000;box-shadow:3.5px 3.5px 5px #000000;background-color:#FFFFFF;border-radius:6px;-moz-border-radius:6px;-webkit-border-radius:6px;border-color:#d4d4d4;padding:0 10px 10px;}
    ul#css3menu1 li:hover>*{
    	display:block;}
    ul#css3menu1 li{
    	position:relative;display:block;white-space:nowrap;font-size:0;float:left;}
    ul#css3menu1 li:hover{
    	z-index:1;}
    ul#css3menu1 ul ul{
    	position:absolute;left:50%;top:0;}
    ul#css3menu1{
    	font-size:0;z-index:999;position:relative;display:inline-block;zoom:1;padding:0;
    	*display:inline;}
    * html ul#css3menu1 li a{
    	display:inline-block;}
    ul#css3menu1>li{
    	margin:0;}
    ul#css3menu1 a:active, ul#css3menu1 a:focus{
    	outline-style:none;}
    ul#css3menu1 a{
    	display:block;vertical-align:middle;text-align:left;text-decoration:none;font:bold 14px Trebuchet MS;color:#000000;text-shadow:#FFF 0 0 1px;cursor:pointer;padding:10px;background-color:#c1c1c1;background-image:url("mainbk.png");background-repeat:repeat;background-position:0 0;border-width:0 0 0 1px;border-style:solid;border-color:#C0C0C0;}
    ul#css3menu1 ul li{
    	float:none;margin:10px 0 0;}
    	
    	
    /*ul#css3menu1 ul a{
    	text-align:left;padding:4px;background-color:#FFFFFF;background-image:none;border-width:0;border-radius:0px;-moz-border-radius:0px;-webkit-border-radius:0px;font:14px Tahoma;color:#000;text-decoration:none;}
    	*/
    ul#css3menu1 ul a.P2 {
    	text-align:left;padding:4px;background-color:#FF9966  ;background-image:none;border-width:0;border-radius:0px;-moz-border-radius:0px;-webkit-border-radius:0px;font:14px Tahoma;color:#000;text-decoration:none;}
    ul#css3menu1 ul a.P1 {
    	text-align:left;padding:4px;background-color:#FFFFFF;background-image:none;border-width:0;border-radius:0px;-moz-border-radius:0px;-webkit-border-radius:0px;font:14px Tahoma;color:#000;text-decoration:none;}
    ul#css3menu1 ul a.P0 {
    	text-align:left;padding:4px;background-color:#FFFFFF;background-image:none;border-width:0;border-radius:0px;-moz-border-radius:0px;-webkit-border-radius:0px;font:14px Tahoma;color:#CCC;text-decoration:none;}
    	
    	
    ul#css3menu1 li:hover>a,ul#css3menu1 li a.pressed{
    	background-color:#f8ac00;border-color:#C0C0C0;border-style:solid;color:#000000;text-shadow:#FFF 0 0 1px;background-image:url("mainbk.png");background-position:0 100px;text-decoration:none;}
    ul#css3menu1 span{
    	display:block;overflow:visible;background-position:right center;background-repeat:no-repeat;padding-right:0px;}
    ul#css3menu1 ul span{
    	background-image:url("arrowsub.png");padding-right:12px;}
    	
    /*ul#css3menu1 ul li:hover>a,ul#css3menu1 ul li a.pressed{
    	background-color:#FFFFFF;background-image:none;color:#868686;text-decoration:none;}*/
    	
    	
    	ul#css3menu1 ul li:hover>a.P1,ul#css3menu1 ul li a.P0.pressed{
    	background-color:#FFFFFF;background-image:none;color:#339966;text-decoration:none;}
    	ul#css3menu1 ul li:hover>a.P0,ul#css3menu1 ul li a.P1.pressed{
    	background-color:#FFFFFF;background-image:none;color:#FF9999;text-decoration:none;}
    	ul#css3menu1 ul li:hover>a.P0,ul#css3menu1 ul li a.P2.pressed{
    	background-color:#FF9966 ;background-image:none;color:#FF9999;text-decoration:none;}
    	
    	
    ul#css3menu1 li.topfirst>a{
    	border-radius:5px 0 0 5px;-moz-border-radius:5px 0 0 5px;-webkit-border-radius:5px;-webkit-border-top-right-radius:0;-webkit-border-bottom-right-radius:0;}
    ul#css3menu1 li.toplast>a{
    	border-radius:0 5px 5px 0;-moz-border-radius:0 5px 5px 0;-webkit-border-radius:0;-webkit-border-top-right-radius:5px;-webkit-border-bottom-right-radius:5px;}
    Last edited by Arcticwarrio; 03-05-2013 at 08:22 AM. Reason: Added Stylesheet
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •