PHP - Dynamic Drop Down Menu

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carlkevin
    New Member
    • Dec 2007
    • 7

    #16
    would anyone send me an example code for drop down menus???at least 3 drop down???just an example???thank s

    Comment

    • carlkevin
      New Member
      • Dec 2007
      • 7

      #17
      could you send me the entire code for these menus? I'm new to PHP and need to create a 3 -field menu derived from 3 DB dependent tables
      Thanks

      Comment

      • stevemanser
        New Member
        • Apr 2006
        • 4

        #18
        I am pasting some code here in case you wanted to see how you can use database items to create a Drop Down Menu instead of a Drop Down List. The menu uses CSS to show/hide elements.

        Please note the database used in the example is not MySQL but txt-db-api, which is a text-file database available for php.




        Code:
        <?//horizontal menu bar *****************************************************************
        $rs1=$db->executeQuery("SELECT * FROM sch_pages WHERE p_parpage=0 ORDER BY p_order");
        while($rs1->next())
        {
        list($p_id, $p_name, $p_sysname, $p_order, $p_prot, $p_pass, $p_nomenu, $p_coded, $p_https, $p_parpage)=$rs1->getCurrentValues();
        if($p_nomenu==0)
        {
        ?>
        
        	
        <ul id="nav">
        	<li><a HREF="index.php?sch_action=display_content&c_page=<?echo $p_id?>"><? echo $p_name ?></a>
        		<ul>    
        
            		
              <?//drop down submenu bar ***************************************************************
              $rs2=$db->executeQuery("SELECT * FROM sch_pages WHERE p_parpage=$p_id ORDER BY p_order");
              while($rs2->next())
              {
              list($p_id, $p_name, $p_sysname, $p_order, $p_prot, $p_pass, $p_nomenu, $p_coded, $p_https, $p_parpage)=$rs2->getCurrentValues();
              if($p_nomenu==0){?>    			
        			  <li><a HREF="index.php?sch_action=display_content&c_page=<?echo $p_id?>"><? echo $p_name ?></a></li>    			
        			<?}
              }
              ?>
              
                       
        		</ul>
        	</li>	
        </ul>
        
        
        <?
        }
        }
        ?>
        Code:
        <script>
        sfHover = function() {
        	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
        	for (var i=0; i<sfEls.length; i++) {
        		sfEls[i].onmouseover=function() {
        			this.className+=" sfhover";
        		}
        		sfEls[i].onmouseout=function() {
        			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
        		}
        	}
        }
        if (window.attachEvent) window.attachEvent("onload", sfHover);
        </script>
        Code:
        <style>
        #nav, #nav ul {
        	padding: 0;
        	margin: 0;
        	list-style: none;
        }
        #nav a {
        	display: block;
        	width: 10em;
        }
        #nav li {
        	float: left;
        	width: 10em;
        }
        #nav li ul {
        	position: absolute;
        	width: 10em;
        	left: -999em;
        }
        #nav li:hover ul {
        	left: auto;
        }
        #nav li:hover ul, #nav li.sfhover ul {
        	left: auto;
        }
        </style>
        Last edited by Atli; Oct 9 '09, 09:13 PM. Reason: Added [code] tags.

        Comment

        • agun
          New Member
          • Oct 2008
          • 16

          #19
          Hi moderator,

          Sorry for OOT,
          Is it allowed here to post some advertisements in the middle of discussion like above?

          Thanks

          <Answer: No it is not. It's been removed. --Atli>

          Comment

          • kodosai
            New Member
            • Feb 2009
            • 1

            #20
            Yes, it can...

            Originally posted by didgy58
            would the following code work if say i had one simple database 'mp3 players'

            and the 1st select box has been hard coded to have 3 manufacturers held within it, after selecting the 1st select the next one is generated from that just getting the name of the items i.e. 1select APPLE 2nd select gets things such as ipod nano, ipod touch etc, the examples above all seem to use multiple databases.

            basically just wondering if it could be implemented on a single database??

            thanks

            dan
            While the previous posts aren't necessary referring to multiple databases, but more than likely multiple tables within a database. Neither is necessary based on your example. You can have one table in a database that has X number of fields. 3 fields in your example..1 for a unique identifier of the record (gid), 1 for the manufacturer (maker), and one for the model (model).

            Then to avoid calling to the database all the time, I would essentially pull the entire database at one go, and populate it out to PHP arrays multi-dimensional arrays.

            Of course depending on volume of data in the table, it may be smarter to do multiple queries to the database depending on selection. Given the relatively small number of players available on the market though, I wouldn't sacrifice the overhead of making the multiple calls.

            Note** There are also many ways to work with internal arrays within PHP with the data, up to you to figure out which one is going to work best for your sitaution, whether you need one large mult-dimensional array, or a grouping of multiple types of arrays.

            Comment

            Working...