ReeceSayer Posted January 30, 2012 Share Posted January 30, 2012 Hi all, I'm just wondering if there's an easier way of doing what accomplishing the following: I have a value in my database which represents a selection in a drop down menu, i want to read it from the database and have it automatically selected depending on the stored data. I have the following working but just wondered if there was an easier way to get the same result: <?php //database connection $query = "SELECT id FROM `tablename` WHERE username='$username'"; $result = mysql_query($query); $row = mysql_fetch_object($result); $id = $row->id; $id = (int)$id; ?> <select name="id"> <option value="">Select your option...</option> <option value="1" <?php if (($id - 1) === 0) { echo 'selected="selected"'; }?>>Selection 1</option> <option value="2"<?php if (($id - 2) === 0) { echo 'selected="selected"'; }?>>Selection 2</option> </select> Sorry if it's not very clear, i'll explain best i can if anyone can help. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/256079-drop-down-menu-and-database-values/ Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2012 Share Posted January 30, 2012 You could store your option value/text pairs in an array, and then loop the array to build the list of <option> tags. Example: $options = array(1=> 'Option1', 'Option2', 'Option3', 'Option4'); echo <select name=\"field_name\">\n"; foreach( $options as $k => $v ) { $selected = $id == $k ? 'selected="selected"' : ''; echo "<option value=\"k\" $selected>" . htmlentities($v) . "</option>\n"; } echo "</select>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/256079-drop-down-menu-and-database-values/#findComment-1312784 Share on other sites More sharing options...
laffin Posted January 30, 2012 Share Posted January 30, 2012 Thats a real bad example there. 1) is your selection boxes always hardcoded, and you are selecting if from a value? Learn to use an array, and loops $opts=array( array('','Select your option...'), array('1','Selection 1'), array('2','Selection 2')); echo '<select name="id">'.PHP_EOL; foreach($opts as $opt) echo ' <option value="'. $opt[0] .'"'. ($id==$opt[0]?$' selected':'') .'>'. $opt[1] .'</option>'.PHP_EOL; echo '</select>'.PHP_EOL; Quote Link to comment https://forums.phpfreaks.com/topic/256079-drop-down-menu-and-database-values/#findComment-1312786 Share on other sites More sharing options...
ReeceSayer Posted January 31, 2012 Author Share Posted January 31, 2012 Hi, Thanks for the quick replies. I thought it was bad which is why i thought i'd post on here to find a better way of doing it but it's the only immediate way i could think of getting the results. I've just had a quick look and it looks like both of your solutions would work. Also yes the dropdown is always hardcoded. I'll try and implement your solutions and see which works best. Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/256079-drop-down-menu-and-database-values/#findComment-1312788 Share on other sites More sharing options...
laffin Posted January 31, 2012 Share Posted January 31, 2012 They both are relatively the same. Concept use an array to build the option list, Pikachu uses the keys as identifiers I went as an all array solution. the selection selector is also the same, just placement is different about only thing is there is a bug in pikachu's code, actually a typo "<option value=\"k\" should be "<option value=\"$k\" Quote Link to comment https://forums.phpfreaks.com/topic/256079-drop-down-menu-and-database-values/#findComment-1312888 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.