PHP - Making Dropdown Menu Sticky
Some forum users here gave me great help yesterday in working with dropdown menus. In fact, I need quite a few of these dropdown menus in several forms over several pages, so I created several simple arrays and made a function to create the dropdowns.
Here's one of my arrays: Code: [Select] <?php $instruments = array( 'Bassoon', 'Cello', 'Clarinet', 'Double Bass', 'Flute', 'French Horn', 'Oboe', 'Percussion', 'Trombone', 'Trumpet', 'Tuba', 'Viola', 'Violin', 'Other' ); Here's my function: Code: [Select] <?php function create_dropdown($array_name, $array_item) { echo "<select name='$array_item'>\n"; echo "<option value='select'>Select…</option>\n"; foreach( $array_name as $v ) { echo "<option value='$v'>" . $v . "</option>\n"; } echo "</select>\n"; } ... and anywhere I need a dropdown menu, I'm calling it like this: Code: [Select] <?php create_dropdown($instruments, 'instrument'); The dropdown menus, however (in some fairly detailed forms for my local youth orchestra's site), need to be sticky. How can I modify my function, above, so that the selected value will be retained if there are other form submission errors when the form is submitted? I have tried endlessly today but to no avail... If you can help while I still have some hair left, I'd be greatly appreciative. Similar TutorialsOkay, really newbie question, but for this code... Code: [Select] <!-- Gender --> <label for="gender">Gender:</label> <select id="gender" name="gender"> <option value="">--</option> <option value="F">Female</option> <option value="M">Male</option> </select> 1.) How do I assign a variable to this? 2.) How do I make this "sticky"? Here is how I have usually done other form types... Code: [Select] <!-- First Name --> <label for="firstName">First Name:</label> <input id="firstName" name="firstName" type="text" maxlength="30" value="<?php if(isset($firstName)){echo htmlentities($firstName, ENT_QUOTES);} ?>" /><!-- Sticky Field --> <?php if (!empty($errors['firstName'])){ echo '<span class="error">' . $errors['firstName'] . '</span>'; } ?> Oh, by the way, at the top of my PHP file I have this code... Code: [Select] if ($_SERVER['REQUEST_METHOD']=='POST'){ // Form was Submitted (Post). // Initialize Errors Array. $errors = array(); // Trim all form data. $trimmed = array_map('trim', $_POST); Thanks, Debbie I have a form created with code already written. I am in need of a push in the right direction or a potential tutorial on this issue i have. I am running a fanatsy golf website where the user will pick one golfer each week and the cannot select them again. Is there a way I can remove that data from the list for the next week when the user makes his selection or can I have that data another color and unselectable. If you want code, please let me know and i can provide it. Thanks in advance for your help. p.s. the list data is stored in a MySQL database. I'm new to this form and php/mysql so sorry if this isn't the right place to post this. This is what is in the first dropdown box. Code: [Select] $selValues['john'] = "a, b, c, d, e"; These are the different lists I want it to put in the second drop down box depending on what they choose in the first. Code: [Select] $selValues['list1']= " a1, a2, a3, a4, a5"; $selValues['list2']= " b1, b2, b3, b4, b5"; This is how I made an attempt to make it work before posting here. Along with plently of other ways. Code: [Select] if ($selValues['john']=a) { $chan_input = selectBuilder($selValues['$list1'] }; else if ($selValues['john']=b) { $chan_input = selectBuilder($selValues['$list2'] }; Basicly how I need it to work is there are two drop down boxes. First they will chose between a,b,c,d,e. If they chose a then on the second drop down box I only want them to be able to select a1,a2,a3,a4,a5. So i have a <drop down> menu, and a <nav> menu on my left side of the page. I have a problem when i click at the first column of my drop down menu and it explores submenu, the submenu mixes with the nav menu that is under the drop down menu on the left. i could solve it with margin-top of the nav menu but i don't like the empty space beetwen them. I tried with putting overflow:visible; in CSS of my dropdown menu but it is still the same. drop down menu is seen but there is stil seen nav menu under and they are mixed. So basicly i want my dropdown menu to be priority, so when i clicks on my first column (only first column is problem because there under is nav menu the other are open nice) it will explore submenu and the part of nav menu that covers the submenu will be hidden.
Here is the screen shot:
Here is the code of head <dropdown> menu if someone finds the problem.
#menu, #menu ul { margin: 0; padding: 0; list-style: none; } #menu { width: 900px; margin-top:20px; margin-left:auto; margin-right:auto; border: 1px solid #222; background-color: #111; background-image: linear-gradient(#444, #111); border-radius: 6px; box-shadow: 0 1px 1px #777; } #menu:before, #menu:after { content: ""; display: table; } #menu:after { clear: both; } #menu { zoom:1; } #menu li { float: left; border-right: 1px solid #222; box-shadow: 1px 0 0 #444; position: relative; } #menu a { float: left; padding: 12px 30px; color: #999; text-transform: uppercase; font: bold 12px Arial, Helvetica; text-decoration: none; text-shadow: 0 1px 0 #000; } #menu li:hover > a { color: #fafafa; } *html #menu li a:hover { /* IE6 only */ color: #fafafa; } #menu ul { margin: 20px 0 0 0; _margin: 0; /*IE6 only*/ opacity: 0; visibility: hidden; position: absolute; top: 38px; left: 0; z-index: 1; background: #444; background: linear-gradient(#444, #111); box-shadow: 0 -1px 0 rgba(255,255,255,.3); border-radius: 3px; transition: all .2s ease-in-out; } #menu li:hover > ul { opacity: 1; visibility: visible; margin: 0; } #menu ul ul { top: 0; left: 150px; margin: 0 0 0 20px; _margin: 0; /*IE6 only*/ box-shadow: -1px 0 0 rgba(255,255,255,.3); } #menu ul li { float: none; display: block; border: 0; _line-height: 0; /*IE6 only*/ box-shadow: 0 1px 0 #111, 0 2px 0 #666; } #menu ul li:last-child { box-shadow: none; } #menu ul a { padding: 10px; width: 130px; _height: 10px; /*IE6 only*/ display: block; white-space: nowrap; float: none; text-transform: none; } #menu ul a:hover { background-color: #0186ba; background-image: linear-gradient(#04acec, #0186ba); } #menu ul li:first-child > a { border-radius: 3px 3px 0 0; } #menu ul li:first-child > a:after { content: ''; position: absolute; left: 40px; top: -6px; border-left: 6px solid transparent; border-right: 6px solid transparent; border-bottom: 6px solid #444; } #menu ul ul li:first-child a:after { left: -6px; top: 50%; margin-top: -6px; border-left: 0; border-bottom: 6px solid transparent; border-top: 6px solid transparent; border-right: 6px solid #3b3b3b; } #menu ul li:first-child a:hover:after { border-bottom-color: #04acec; } #menu ul ul li:first-child a:hover:after { border-right-color: #0299d3; border-bottom-color: transparent; } #menu ul li:last-child > a { border-radius: 0 0 3px 3px; }Here is the code of <nav> menu if someone finds the problem. #cssmenu { width:15%; padding: 0; margin-top: 50px; margin-left:auto; margin right:auto; float:left; border: 0; line-height: 1; } #cssmenu ul, #cssmenu ul li, #cssmenu ul ul { list-style: none; margin: 0; padding: 0; } #cssmenu ul { position: relative; z-index: 597; float: left; } #cssmenu ul li { float: left; min-height: 1px; line-height: 1em; vertical-align: middle; position: relative; } #cssmenu ul li.hover, #cssmenu ul li:hover { position: relative; z-index: 599; cursor: default; } #cssmenu ul ul { visibility: hidden; position: absolute; top: 100%; left: 0px; z-index: 598; width: 100%; } #cssmenu ul ul li { float: none; } #cssmenu ul ul ul { top: -2px; right: 0; } #cssmenu ul li:hover > ul { visibility: visible; } #cssmenu ul ul { top: 1px; left: 99%; } #cssmenu ul li { float: none; } #cssmenu ul ul { margin-top: 1px; } #cssmenu ul ul li { font-weight: normal; } /* Custom CSS Styles */ #cssmenu { width: 200px; background: #333333; font-family: 'Oxygen Mono', Tahoma, Arial, sans-serif; zoom: 1; font-size: 12px; } #cssmenu:before { content: ''; display: block; } #cssmenu:after { content: ''; display: table; clear: both; } #cssmenu a { display: block; padding: 15px 20px; color: #ffffff; text-decoration: none; text-transform: uppercase; } #cssmenu > ul { width: 200px; } #cssmenu ul ul { width: 200px; } #cssmenu > ul > li > a { border-right: 4px solid #1b9bff; color: #ffffff; } #cssmenu > ul > li > a:hover { color: #ffffff; } #cssmenu > ul > li.active a { background: #1b9bff; } #cssmenu > ul > li a:hover, #cssmenu > ul > li:hover a { background: #1b9bff; } #cssmenu li { position: relative; } #cssmenu ul li.has-sub > a:after { content: '+'; position: absolute; top: 50%; right: 15px; margin-top: -6px; } #cssmenu ul ul li.first { -webkit-border-radius: 0 3px 0 0; -moz-border-radius: 0 3px 0 0; border-radius: 0 3px 0 0; } #cssmenu ul ul li.last { -webkit-border-radius: 0 0 3px 0; -moz-border-radius: 0 0 3px 0; border-radius: 0 0 3px 0; border-bottom: 0; } #cssmenu ul ul { -webkit-border-radius: 0 3px 3px 0; -moz-border-radius: 0 3px 3px 0; border-radius: 0 3px 3px 0; } #cssmenu ul ul { border: 1px solid #0082e7; } #cssmenu ul ul a { font-size: 12px; color: #ffffff; } #cssmenu ul ul a:hover { color: #ffffff; } #cssmenu ul ul li { border-bottom: 1px solid #0082e7; } #cssmenu ul ul li:hover > a { background: #4eb1ff; color: #ffffff; } #cssmenu.align-right > ul > li > a { border-left: 4px solid #1b9bff; border-right: none; } #cssmenu.align-right { float: right; } #cssmenu.align-right li { text-align: right; } #cssmenu.align-right ul li.has-sub > a:before { content: '+'; position: absolute; top: 50%; left: 15px; margin-top: -6px; } #cssmenu.align-right ul li.has-sub > a:after { content: none; } #cssmenu.align-right ul ul { visibility: hidden; position: absolute; top: 0; left: -100%; z-index: 598; width: 100%; } #cssmenu.align-right ul ul li.first { -webkit-border-radius: 3px 0 0 0; -moz-border-radius: 3px 0 0 0; border-radius: 3px 0 0 0; } #cssmenu.align-right ul ul li.last { -webkit-border-radius: 0 0 0 3px; -moz-border-radius: 0 0 0 3px; border-radius: 0 0 0 3px; } #cssmenu.align-right ul ul { -webkit-border-radius: 3px 0 0 3px; -moz-border-radius: 3px 0 0 3px; border-radius: 3px 0 0 3px; } Edited by Dorkmind, 26 November 2014 - 05:00 PM. I am trying to make menu with submenu. I have two tables: meni(id_meni, naslov, pozicija, lang_id) and subjects (subjecte_id, naslov, text, meni_id, lang_id, pozicija). Into subjects(meni_id) is written meni(id_meni) value in order to determinate what menu items are having summenu options. How can i do this? I tried following code, but it only display items from meni table: Code: [Select] <?php $query_meni =mysql_query("SELECT * FROM meni WHERE lang_id = '$jezik'"); $query_subject =mysql_query("SELECT * FROM subjects WHERE lang_id = '$jezik'"); while($row_meni = mysql_fetch_array($query_meni)){ echo "<li>"; echo "<a href=\"index.php?page="; echo $row_meni['id_meni']; echo "\">"; echo $row_meni['naslov']; echo "</a>"; while($row_subjects = mysql_fetch_array($query_subject)){ if($row_meni['id_meni'] == $row_subjects['meni_id']){ echo "<ul class=\"subnav\">"; echo "<li>"; echo "<a href=\"index.php?subject="; echo $row_subjects['subject_id']; echo "\">"; echo $row_subjects['naslov']; echo "</li>"; echo "</a>"; echo "</ul>"; } } echo "</li>"; } ?> Hi i have a online/offline script that works great no issues at all. In order to change the html, php file to display when offline I need to point it to that file via input field. What i would like is to change the input filed and have a dropdown that list all the folders and subfolders within the the directory where all the templates are located. I have found a php snippet on the forum that does exactly what i want. The issue is now is to remove the input filed in the script and insert the snippet found here and thats where the issue comes. I'm hoping some guru here can easily help me out . please see below for the code online/offline code ( input filed i would like to replace with dropdown selector <input type="text" name="txttemplate" value="%s" id="txttemplate" />) Code: [Select] <?php session_start(); $page_offline = TRUE; $page_offline_title = " "; $page_offline_message = " Offline"; $admin_ip = "1.1.1.1"; $admin_password = "*****************"; $page_offline_template = "template/path/index.php"; $page_offline_foradmin = TRUE; ini_set("display_errors", "0"); error_reporting(E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR); $page = strstr($_SERVER['PHP_SELF'], "off.php") ? "admin" : "website"; // Try login $login_error = FALSE; if(isset($_POST['txtpassword'])){ if(md5($_POST['txtpassword']) == $admin_password){ $_SESSION['offline_admin'] = TRUE; }else{ $login_error = TRUE; } } // Save config settings $offline = $page_offline ? 'checked="checked"' : ""; $title = $page_offline_title; $message = $page_offline_message; $saved = FALSE; $template = $page_offline_template; $adminoffline = $page_offline_foradmin ? 'checked="checked"' : ""; if(isset($_POST['btnsave']) && $_SESSION['offline_admin']){ $val_offline = isset($_POST['chkoffline']) && ($_POST['chkoffline'] == "true") ? TRUE : FALSE; $val_title = htmlspecialchars($_POST['txttitle']); $val_message = htmlspecialchars($_POST['txtmessage']); $val_template = htmlspecialchars($_POST['txttemplate']); $val_password = htmlspecialchars($_POST['txtpassword']); $val_adminoffline = isset($_POST['chkconstruct']) && ($_POST['chkconstruct'] == "true") ? TRUE : FALSE; $offline = $val_offline ? 'checked="checked"' : ""; $title = $val_title; $message = $val_message; $template = $val_template; $adminoffline = $val_adminoffline ? 'checked="checked"' : ""; if(empty($val_password)){ page_offline_save($val_offline, $val_title, $val_message, $val_template, $adminoffline); }else{ page_offline_save($val_offline, $val_title, $val_message, $val_template, $adminoffline, $val_password); } $saved = TRUE; } // Templates $admin_style_tpl = <<<ADMINSTYLETPL body{ font-family:Arial, Helvetica, sans-serif; font-size:11px; text-align: center; background-color:#F9F9F9; color:#555555; } #wrapper{ width:400px; padding:20px; margin: 0 auto; border:1px solid #DADADA; background-color: #FFF; text-align: left; } h2{ margin: 0px 0px 10px 0px; padding: 0px; font-size: 24px; color:#464646; font-family:Georgia,"Times New Roman",Times,serif; font-style: italic; font-weight: normal; } label{ display: block; width: 150px; font-weight: bold; padding: 14px 0px 3px 0px; } input{ -moz-border-radius-bottomleft:3px; -moz-border-radius-bottomright:3px; -moz-border-radius-topleft:3px; -moz-border-radius-topright:3px; width: 300px; background:#F5F5F5 none repeat scroll 0 0; border:1px solid #CCCCCC; color:#666666; padding: 4px 8px; } #chkoffline, #chkconstruct{ width: 20px; } .error{ border: solid 1px #CC0000; } ADMINSTYLETPL; $admin_login_tpl = <<<ADMINLOGINTEMPLATE <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Login</title> <style> %s </style> </head> <body> <div id="wrapper"> <h2>Login</h2> <form id="form1" name="form1" method="post" action=""> <label for="txtpassword">Password</label> <input type="password" name="txtpassword" %s id="txtpassword" /><br /><br /> <input name="btnlogin" type="submit" value="Login" /> </form> </div> </body> </html> ADMINLOGINTEMPLATE; $admin_config_tpl = <<<ADMINCONFIGTEMPLATE <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style> %s </style> </head> <body> <div id="wrapper"> <h2></h2> <form id="frmadmin" name="frmadmin" method="post" action=""> <label for="chkoffline">Offline</label> <input name="chkoffline" id="chkoffline" %s type="checkbox" value="true" /> <label for="chkconstruct">Show offline to admin</label> <input name="chkconstruct" id="chkconstruct" %s type="checkbox" value="true" /> <label for="txttitle">Title</label> <input type="text" name="txttitle" value="%s" id="txttitle" /> <label for="txtmessage">Message</label> <input type="text" name="txtmessage" value="%s" id="txtmessage" /> <label for="txttemplate">Template</label> <input type="text" name="txttemplate" value="%s" id="txttemplate" /> <label for="txtpassword">Admin password</label> <input type="password" name="txtpassword" id="txtpassword" /> <br /> Leave this field empty if you don't want to change the admin password. <br /><br /> <input name="btnsave" type="submit" value="Save" /> </form> </div> </body> </html> ADMINCONFIGTEMPLATE; $admin_saved_tpl = <<<ADMINSAVEDTEMPLATE <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Offline Admin</title> <style> %s </style> </head> <body> <div id="wrapper"> <h2>Configuration saved</h2> The configuration is saved succesfully. You can continue to <a href="off.php">config</a> or go to your <a href="../">Homepage</a> or back to <a href="index.php">admin panel</a>. </div> </body> </html> ADMINSAVEDTEMPLATE; if($page == "admin"){ // If user not is loggedin then show login screen if(!isset($_SESSION['offline_admin']) || !$_SESSION['offline_admin']){ echo sprintf($admin_login_tpl, $admin_style_tpl, ($login_error) ? 'class="error"' : ''); }else{ if(!$saved){ echo sprintf($admin_config_tpl, $admin_style_tpl, $offline, $adminoffline, $title, $message, $template); }else{ echo sprintf($admin_saved_tpl, $admin_style_tpl); } } exit(); }else{ if($page_offline && (($_SERVER['REMOTE_ADDR'] != $admin_ip) || $page_offline_foradmin)){ ob_start(); include($page_offline_template); $template = ob_get_contents(); ob_end_clean(); // Replace variables $template = str_replace(array("{TITLE}", "{MESSAGE}"), array($page_offline_title, $page_offline_message), $template); // Show template echo $template; exit(); } } // Write settings to file function page_offline_save($offline, $title, $message, $template, $adminoffline, $password=""){ $lines = explode("\n", file_get_contents("offline.php")); // Set configuration $offline_state = ($offline) ? "TRUE" : "FALSE"; $lines[2] = '$page_offline = ' . $offline_state . ';'; $lines[3] = '$page_offline_title = "' . $title . '";'; $lines[4] = '$page_offline_message = "' . $message . '";'; $lines[5] = '$admin_ip = "' . $_SERVER['REMOTE_ADDR'] . '";'; $lines[7] = '$page_offline_template = "' . $template . '";'; $adminoffline_state = ($adminoffline) ? "TRUE" : "FALSE"; $lines[8] = '$page_offline_foradmin = ' . $adminoffline_state . ';'; if(!empty($password)){ $lines[6] = '$admin_password = "' . md5($password) . '";'; } // Save to file file_put_contents("off.php", implode("\n", $lines)); } ?> snippet found here on the forum Code: [Select] <?php $parent_directory = 'path/to/directory'; $file_types = 'html,htm,php,etc'; //===================================================// // FUNCTION: directoryToArray // // // // Parameters: // // - $root: The directory to process // // - $to_return: f=files, d=directories, b=both // // - $file_types: the extensions of file types to // // to return if files selected // //===================================================// function directoryToArray($root, $to_return='b', $file_types=false) { $array_items = array(); if ($file_types) { $file_types=explode(',',$file_types); } if ($handle = opendir($root)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $add_item = false; $type = (is_dir($root. "/" . $file))?'d':'f'; $name = preg_replace("/\/\//si", "/", $file); if ($type=='d' && ($to_return=='b' || $to_return=='d') ) { $add_item = true; } if ($type=='f' && ($to_return=='b' || $to_return=='f') ) { $ext = end(explode('.',$name)); if ( !$file_types || in_array($ext, $file_types) ) { $add_item = true; } } if ($add_item) { $array_items[] = array ( 'name'=>$name, 'type'=>$type, 'root'=>$root); } } } // End While closedir($handle); } // End If return $array_items; } if (isset($_POST[pickfile])) { // User has selected a file take whatever action you want based // upon the values for folder and file } else { echo ' <html> <head> <script type="text/javascript"> function changeFolder(folder) { document.pickFile.submit(); } </script> </head> <body>'; echo "<form name=\"pickFile\" method=\"POST\">\n"; $directoryList = directoryToArray($parent_directory,'d'); echo "<select name=\"folder\" onchange=\"changeFolder(this.value);\">\n"; foreach ($directoryList as $folder) { $selected = ($_POST[folder]==$folder[name])? 'selected' : ''; echo "<option value=\"$folder[name]\" $selected>$folder[name]</option>\n"; } echo '</select><br><br>'; $working_folder = ($_POST[folder]) ? $_POST[folder] : $directoryList[0][name]; $fileList = directoryToArray($parent_directory.'/'.$working_folder,'f',$file_types); echo "<select name=\"file\">\n"; foreach ($fileList as $file) { echo "<option value=\"$file[name]\">$file[name]</option>\n"; } echo '</select><br><br>'; echo "<button type=\"submit\" name=\"pickfile\">Submit</button>\n"; echo "</form>\n"; echo "</body>\n"; echo "</html>\n"; } ?> Thank you in advance for any help... Hi all, I was wondering if someone knows a clean way to make a dropdown menu using just php and html with the following functionality. If someone selects a value from the dropdown options it should be visible on refresh. So in case a have a dropdown menu with: oranges, apples, grapes and someone selects grapes and submits it, grapes should be selected. I have made someonthing but it looks redundant. I think there should be something with a foreach loop or something but i can't figure it out yet. Thanks in advance, cheers!! I want make the following, (I have already a database with three tables (Countries, Timeline and Category)). 1: list of countries (drop down menu 1), Timeline of the countries history (drop down 2) and Category (drop down 3). 2: The selected values of the drop down menus must show take the information from the database. Can any one help me with the coding? how do i this? i have no idea....i need to incorporate it on an html form pleas help? i will get the data from this line and must be able to select one from the dropdown and get the id/name from the one selected [COLOR="Red"]$supplier_names = LpmAdnetworkPeer::getByName($catcher_id);[/COLOR] thank you Hello,
I'm new to php, I don't really have much experience, just have used it in combination with dreamweaver. I'm tackling a new problem now.
Basically, it's about a dropdown menu with pre-defined values. There are multiple dropdowns, having different value choices. People are supposed to be able to select more than one option and then search for the results. these should then be generated according to the selected values, or if no value is selected, ALL is shown.
When the user clicks on search, the values create a msql query that will then be executed and the results shown on the page.
I've been searching high and low for code ideas, but see different stuff everywhere I look, and none of the code seems to be what i want. Some people are using Javascript, some Ajax, or PHP. ideally, I want to use just php and sql
Can someone point me in the right direction on how to tackle this problem, or maybe someone has some code ideas?
Thanks in advance.
Mio
Hi, I don't know if this is the right place, but I used a code for a dropdown menu witch works fine on the website (geloven.eu/ABK2021) who uses PHP 7.2.24, but the dropdown does NOT work on another site bijbelkamp.eu/ABK2021 who uses PHP 7.3.28. The index page just includes the menu.php (here menu.txt) and the css file bootstrap21.css (here bootstrap21.txt). Can this be some coding issue or does this have nothing to do with the code? bootstrap21.txt menu.txt I think after a lot of trouble I've finally managed to get my hierarchical php driven hierarchical dropdown menu working. I now have 1 problem with it which I simply can't figure out. Rather than explain this I think a picture is worth a thousand words so here's the script in action:- http://www.coolvisiontest.com/garyssite/rhrvouchers/menu4.php If you notice in that example when you hover over categories then level1a then level2a the menu's and their children appear 1 at a time as they should. However if you hover over level1a then level2b then all at once all of the child categories of 2b and all of their child catagories and their child categories etc appear at once rather than one at a time. You should have to hover over level3e to see level4a and you should have to hover over level4a to see level5a etc. The code I have is a combination of 2 sets of code I came across to be honest so I'm having trouble figuring it out. At the moment I only have 3 problems with it:- 1. I only want subcategories to be shown when their parent category is hovered over as explained above 2. I only want the bottom level of categories to act as links e.g. levels 2c 3a 3b 3c 3d and 5a would act as links in this example as they are the last of all the children, the rest of the categories should be plain text. I'm not sure if this is achievable though as I think links are required for the hover functionality. 3. I want some way of indicating to the user that a category has children perhaps by including a little right arrow or plus sign and of course this should only be included for the categories that have children and not the others. Can anyone help me out with these three things please? Here's my code so far:- <?php $link = mysqli_connect('localhost','',''); mysqli_select_db($link,''); $query = mysqli_query($link,'SELECT * FROM nested_categories'); while ( $row = mysqli_fetch_assoc($query) ){ $menu_array[$row['id']] = array('id' => $row['id'],'name' => $row['name'],'parent' => $row['parent_id']); } function generate_menu($parent){ $has_childs = false; global $menu_array; foreach($menu_array as $key => $value){ if ($value['parent'] == $parent){ if ($has_childs === false){ $has_childs = true; echo '<ul>'; } echo '<li><a href="menu4.php?id='.$value['id'].'">'.$value['name'].'</a>'; generate_menu($key); echo '</li>'; } } if ($has_childs === true) echo '</ul>'; } ?> <div id="menu"> <ul> <li><h2>categories</h2> <?php generate_menu(0); ?> </li> </ul> </div> And the Css:- #menu { width: 100%; background: #eee; float: left; } #menu ul { list-style: none; margin: 0; padding: 0; width: 12em; float: left; } #menu a, #menu h2 { font: bold 11px/16px arial, helvetica, sans-serif; display: block; border-width: 1px; border-style: solid; border-color: #ccc #888 #555 #bbb; margin: 0; padding: 2px 3px; } #menu h2 { color: #fff; background: #000; text-transform: uppercase; } #menu a { color: #000; background: #efefef; text-decoration: none; } #menu a:hover { color: #a00; background: #fff; } #menu li {position: relative;} #menu ul ul { position: absolute; z-index: 500; } #menu ul ul ul { top: 0; left: 100%; } div#menu ul ul, div#menu ul li:hover ul ul, div#menu ul ul li:hover ul ul {display: none;} div#menu ul li:hover ul, div#menu ul ul li:hover ul, div#menu ul ul ul li:hover ul {display: block;} hello everybody..
i need help..
i want to make a dropdown menu with php coding that will show the result from my database.. can anyone help me to generate the coding?
this is an example form:
<form action="" method="get"><select name=""> <option value="select">select</option> <option value="1">1 </option> <option value="2">2</option> <option value="3">3</option> </select> <label> <input type="submit" name="button" id="button" value="Submit" /> </label> </form> if user select 1, the php code will show a list of name by their identification card, if user select 2 the php code will show a list of name by country. Using unordered list, <ul> Reading from XML file, Taking menu name from database, Is there other system to make dropdown navigation menu? Which is the best way to make dropdown navigation menu? I also want to show the sitemap of navigation.How can it be done? I have a dropdown menu in a form where users must select their instrument: here's the dropdown: Code: [Select] <label for="instrument"><span class='red'>*</span>Your main instrument</label> <select name="instrument" id="instrument"> <option value="select" selected="selected">Select your instrument…</option> <option value="bassoon">Bassoon</option> <option value="cello">Cello</option> <option value="clarinet">Clarinet</option> <option value="double_bass">Double Bass</option> <option value="flute">Flute</option> <option value="french_horn">French Horn</option> <option value="oboe">Oboe</option> <option value="percussion">Percussion</option> <option value="trombone">Trombone</option> <option value="trumpet">Trumpet</option> <option value="tuba">Tuba</option> <option value="viola">Viola</option> <option value="violin">Violin</option> <option value="other">Other</option> </select> I want to validate to ensure that the user has selected an instrument from the list. I was going to do something like this: Code: [Select] // validate for instrument selection if(!isset($_POST['instrument'])) { $serrors[] = 'Please select your instrument.'; } else { $_SESSION['instrument'] = $_POST['instrument']; } The problem is, the session variable gets set if the user bypasses the menu altogether, leaving the default 'Select your instrument...' option (at the top) as it is. How can I ensure a selection other than the 'Select your instrument...' at the top of the list? Thanks in advance. Hi, I'm trying to get data from one field in a table (database). But I get undesirable result: Here is my code -> <?php $result2 = mysql_query("SELECT DISTINCT theme FROM mytable ") or die(mysql_error()); while($row2 = mysql_fetch_array( $result2 )) { ?> <form method="post" action='<?php echo $_SERVER["PHP_SELF"]; ?>'> <select name='themes'"> <?php $arr= array($row2['theme']); foreach($row2 as $value) { echo "<option value='$value'><b>". $value."</b> </option><br> "; } } ?> The attached image file show the result that I don't wont. (It's not a dropdown). Is there anyone who may help me, I spent a lot of time to find out but I can't. Thanks a lot for your help I'm not sure what to search for or if this can be done... Let's say I have a mysql table named "genre". Now I have "Male" and "Female" in a dropdown menu like this in a form: <select name="genre"> <option>Male</option> <option>Female</option> </select> How would you display, for example, the option Female in a update.php file if that's the genre stored in the mysql database when you fetch the results? I'm attempting to create a dropdown hover menu by following the instructions on the Suckerfish site. The list that is supposed to dropdown simply stays visible all the time. Is there anything in the code that seems to be the apparent cause of the problem? You will be able to tell that I'm new to PHP. Any suggestions would be appreciated. The following is my CSS and PHP code: CSS: Code: [Select] #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; } PHP/JavaScript: Code: [Select] function draw_app_menu() { // draw the application menu ?> <!-- left - background-color: #cdeb8b - lime --> [color=blue]<script type="text/javascript"> shHover = 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>[/color] [color=green]<ul id = "nav">[/color] <?php session_start(); if (isset($_SESSION['selected_app'])) { // echo '<p> selected_app was set </p>'; foreach ($_SESSION['application_list'] as $app_list) { if (($app_list[5] == 'A') && ($app_list[0] == $_SESSION['selected_app'])) { $_SESSION['current_link'] = $app_list[2]; } if (($app_list[0] == $_SESSION['selected_app']) && (($app_list[5] == 'M') || ($app_list[5] == 'S'))) { if ($app_list[5] == 'M') { ?> [color=green]<li><a href="<?php echo $_SESSION['current_link'].'?id='.$_SESSION['selected_app'].'&modid='.$app_list[3].'&func='.$app_list[6];?>"><?php echo $app_list[1];?></a></li>[/color] <?php $_SESSION['selected_mod'] = $_GET['modid']; $_SESSION['app_function'] = $_GET['func']; } ?> [color=green] <ul>[/color] <?php if ($app_list[5] == 'S') { ?> [color=green]<li><a href="<?php echo $_SESSION['current_link'].'?id='.$_SESSION['selected_app'].'&modid='.$app_list[3].'&submodid='.$app_list[4].'&func='.$app_list[6];?>"><?php echo $app_list[1];?></a></li>[/color] <?php $_SESSION['selected_mod'] = $_GET['modid']; $_SESSION['selected_submod'] = $_GET['submodid']; $_SESSION['app_function'] = $_GET['func']; } ?> [color=green]</ul> </li>[/color] <?php } } } ?> [color=green]</ul>[/color] <script src="sfhover.js" type="text/javascript"></script> <?php } Hello I am having a trouble with creating page that will search records from database using dropdown menu, when I select a category, for example By: Gender and after clicking the Submit button, there is no records displays but when I select By: ALL, the records displays. What's wrong with my code? I'm sorry I'm fairly new to PHP. Here is my code: Code: [Select] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Search</title> </head> <body> <form action="" method="POST" name="records"> <label>SEARCH: </label> <select name="select"> <option value="all" selected="selected">ALL</option> <option value="year">By Year</option> <option value="gender">By Gender</option> <option value="course">By Course</option> </select> <input name="submit" value="GO" type="submit" /> <br /> </form> </body> </html> <?php include('collegeinfo_connect.php'); mysql_connect("$server", "$user", "$pass")or die("cannot connect"); mysql_select_db("$db")or die("cannot select DB"); if (isset($_POST['submit'])) { $selection = $_POST['select']; if($selection == "all") $query = "SELECT * FROM collegeinfo_tbl"; else if($selection == "year") $query = "SELECT * FROM collegeinfo_tbl WHERE Year='year'"; else if($selection == "gender") $query = "SELECT * FROM collegeinfo_tbl WHERE Gender='gender'"; else if($selection == "course") $query = "SELECT * FROM collegeinfo_tbl WHERE Course='course'"; } if (isset($query)) { $result = mysql_query($query) or die(mysql_error()); echo "<br /> <br />"; echo "<table border='1' cellpadding='10'>"; echo "<tr> <th>ID Number:</th> <th>First Name:</th> <th>Last Name:</th> <th>Gender:</th> <th>Year:</th> <th>Course:</th> </tr>"; while ($row = mysql_fetch_array($result)) { echo "<tr>"; echo '<td>' . $row['ID'] . '</td>'; echo '<td>' . $row['FirstName'] . '</td>'; echo '<td>' . $row['LastName'] . '</td>'; echo '<td>' . $row['Gender'] . '</td>'; echo '<td>' . $row['Year'] . '</td>'; echo '<td>' . $row['Course'] . '</td>'; echo "</tr>"; } echo "</table>"; } ?> Hi All, I am trying create 4 dropdowns and load the values based on the value selected in the previous dropdown. I am able to do this for 3 dropdowns, but for the fourth dropdown I am no able to populate the value. Could someone help me out with this please? --------------------------------------- ajaxData_1.php <?php // Include the database config file include_once 'dbconfig_1.php'; if(!empty($_POST["solutions_id"])){ // Fetch state data based on the specific country $query = "SELECT * FROM releases WHERE solutions_id = ".$_POST['solutions_id']; $result = $db->query($query); if($result->num_rows > 0){ echo '<option value="">Select release</option>'; while($row = $result->fetch_assoc()){ echo '<option value="'.$row['releases_id'].'">'.$row['releases_name'].'</option>'; } } else{ echo '<option value="">Release not available</option>'; } } elseif(!empty($_POST["releases_id"])){ $query = "SELECT * FROM versions WHERE releases_id = ".$_POST['releases_id'].""; $result = $db->query($query); if($result->num_rows > 0){ echo '<option value="">Select version</option>'; while($row = $result->fetch_assoc()){ echo '<option value="'.$row['version_id'].'">'.$row['supported_version'].'</option>'; } } else{ echo '<option value="">Version not available</option>'; } } elseif(!empty($_POST["versions_id"])) { $query = "SELECT * FROM platforms WHERE version_id = ".$_POST['version_id'].""; $result = $db->query($query); if($result->num_rows > 0){ echo '<option value="">Select Version</option>'; while($row = $result->fetch_assoc()){ echo '<option value="'.$row['platforms_id'].'">'.$row['platforms_name'].'</option>'; } } else{ echo '<option value="">Platform not available</option>'; } } ?> <?php if(isset($_POST['submit'])) { echo 'Selected Solution ID: '.$_POST['solution']; echo 'Selected Release ID: '.$_POST['release']; echo 'Selected Version ID: '.$_POST['version']; echo 'Selected Platform ID: '.$_POST['platform']; } ?>
Index_3.php <!DOCTYPE html> <html> <head> <title style="color: 000000">Solution Compatibility Matrix</title> <style> select { padding: 12px; min-width:280px; margin-top:10px; } .san{ width:280px; margin:0 auto; margin-top:90px; background-color:#FFFFFF; padding:55px; color: 000000; } label { color:#000000; margin-bottom:25px; } html{ background-color: #3399CC; } </style> </head> <body> </body> </html>
Thanks, Arun Edited April 23, 2020 by cyberRobotAdded code tags |