rocky48 Posted April 7, 2015 Share Posted April 7, 2015 How can I display the Week No as well as the Week Commencing date in a dropdown list? Here is the code: <?php include("connect_visits.php"); //connect to database doDB7(); //Function to build select options based on passed array $WeekNos[] = array($WkNo_info['WNo'], $WkNo_info['WCom']); buildSelectOptions($WeekNos); function buildSelectOptions($options) { $optionsHTML = "<select name=\"WeekNo\">\r\n"; foreach($options as $id => $label) { $optionsHTML .= "<option value='{$id}'>{$label}</option>\n"; } return $optionsHTML; } //Run query to get the Week No and Week Commencing from the table //Then populate into an array $WkNoList_sql = "SELECT WNo, WCom FROM WeekNo15"; $WkNoList_res = mysqli_query($mysqli, $WkNoList_sql) or die(mysqli_error($mysqli)); if (mysqli_num_rows($WkNoList_res) < 1) { //this Week No does not exist $display_block = "<p><em>You have selected an invalid Week.<br/> Please try again.</em></p>"; } $WeekNos = array(); while($WkNo_info = mysqli_fetch_array($WkNoList_res)) { $WeekNos [$WkNo_info['WNo']] = $WkNo_info['WCom']; } $WNo_Options = buildSelectOptions($WeekNos); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- Design by Free CSS Templates http://www.freecsstemplates.org Released for free under a Creative Commons Attribution 2.5 License Name : Yosemite Description: A two-column, fixed-width design with dark color scheme. Version : 1.0 Released : 20091106 --> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="keywords" content="" /> <meta name="description" content="" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>1066 Cards 4U - Stats Menu</title> <link href="style.css" rel="stylesheet" type="text/css" media="screen" /> </head> <body> <div id="wrapper"><!-- start #wrapper --> <div id="menu"><!-- start #menu --> <ul> <li class="current_page_item"><a href="index.php">Home</a></li> <li><a href="Links.html">Links</a></li> <li><a href="Verse_Menu.html">Verses</a></li> <li><a href="Techniques.html">Techniques</a></li> <li><a href="blog.php">Blog</a></li> <li><a href="Gallery.html">Gallery</a></li> <li><a href="contact.html">Contact</a></li> <li><a href="AboutUs.html">About Us</a></li> <li><a href="Stats_Menu.html">Stats</a></li> </ul> </div><!-- end #menu --> <div id="header"><!-- start #header --> <div id="logo"> <!-- start logo --> <h1><a href="http://www.1066cards4u.co.uk">1066 Cards 4U</a></h1> </div><!-- end logo --> </div><!-- end #wrapper --> </div><!-- end #header --> <div id="page"><!-- start page --> <div id="page-bgtop"><!-- start page-bgtop --> <div id="page-bgbtm"><!-- start page-bgbtm --> <div id="content"><!-- start #content --> <h2>Stats for Week Commencing for the year 2015</h2> <p>Please select Week Commencing Date to get the stats</p> <p><form action="VByWeek15.php" method="post"> Week Commencing: <SELECT name="WeekNo"> <option value= "<?php echo $WNo_Options;?>" </option> </select> </br></br> <input type="submit" value="Submit Choice"> </form></p> </div><!-- end #content --> <div id="sidebar"><!-- start #sidebar --> <div id="search" ><!-- start #search --> <h2><font color="blue">SITE SEARCH</h2> <script> (function() { var cx = '018330043583532205772:h1v3z6ucuna'; var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true; gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s); })(); </script> <gcse:search></gcse:search> </div><!-- end #search --> <div style="clear: both;"> </div> <br /><br /><br /><br /> <p>We LOVE 1066 Country!<br /> Visit the UK and come to 1066 Country. </p> </div><!-- end #sidebar --> <div style="clear: both;"> </div> </div><!-- end #page-bgtop --> </div><!-- end #page-bgbtm --> </div><!-- end #page --> <div id="footer"><!-- start #footer --> <p>Copyright (c) 2008 Sitename.com. All rights reserved. Design by <a href="http://www.freecsstemplates.org/" rel="nofollow">FreeCSSTemplates.org</a>.</p> </div><!-- end #footer --> </body> </html> The users find it difficult to relate the dates to the week nos hence my wish to change, Quote Link to comment https://forums.phpfreaks.com/topic/295343-display-2-variables-in-dropdown-list/ Share on other sites More sharing options...
ginerjm Posted April 7, 2015 Share Posted April 7, 2015 Simply add the other field to the displayed portion of the drop down. $optionsHTML .= "<option value='$id'>$id - $label</option>\n"; (Note the removal of the unnecessary braces on your vars.) Quote Link to comment https://forums.phpfreaks.com/topic/295343-display-2-variables-in-dropdown-list/#findComment-1508462 Share on other sites More sharing options...
Solution Psycho Posted April 7, 2015 Solution Share Posted April 7, 2015 (edited) (Note the removal of the unnecessary braces on your vars.) Just because something is not required does not mean it isn't "needed". I always put braces around my variables within double quoted text as a standard. That way I never have to worry about the times that not bracing the variable would cause a problem. EDIT: After reviewing the OPs code, I think the challenge he has is that he is using a function to create the select lists. Assuming that function is used for more than just this one specific list, he may not want the "id" included in the label for all the select lists. In that case, you can get the desired results by ensuring the input array for the function has the data you want before you call the function. Then there is no need to modify the function if it is working as you need it for other select lists. $WeekNos = array(); while($WkNo_info = mysqli_fetch_array($WkNoList_res)) { $WeekNos [$WkNo_info['WNo']] = "{$WkNo_info['WNo']} - {$WkNo_info['WCom']}"; } $WNo_Options = buildSelectOptions($WeekNos); Edited April 7, 2015 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/295343-display-2-variables-in-dropdown-list/#findComment-1508463 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.