leachus2002 Posted February 9, 2011 Share Posted February 9, 2011 Hi There, Is there any way that I can get a select box to auto-select the value from a SQL query? For example, if I have a select box like this: Dave,Jon,Simon,Fred And a select statement that brings back the value of Fred then the select box (in HTML) would: <select name='people'><option>Dave</option><option>Jon</option><option>Simon</option><option selected='selected'>Fred</option></select> Same as if the name was Dave, or Simon etc etc. Hope that makes sense? Cheers Matt Quote Link to comment https://forums.phpfreaks.com/topic/227162-working-with-select-boxes/ Share on other sites More sharing options...
MatthewJ Posted February 9, 2011 Share Posted February 9, 2011 You just wrote the logic in your head... so you get the data, then you write the select statement code... Then, within each option add if($datafield == "whicheveroption") { echo "selected='selected'"; } Quote Link to comment https://forums.phpfreaks.com/topic/227162-working-with-select-boxes/#findComment-1171794 Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 You'd need to use an if statement. Are all of the names in your database already? If you put the names into an array and store the selected one as a variable, it's a simple foreach to loop through them, and an if to check if it's selected. Quote Link to comment https://forums.phpfreaks.com/topic/227162-working-with-select-boxes/#findComment-1171795 Share on other sites More sharing options...
leachus2002 Posted February 9, 2011 Author Share Posted February 9, 2011 Hi Both, Thanks for coming back to me! The complication is that my select box if built using an SQL statement. Like the following: while ($rowvalues=mssql_fetch_array($getnames)){ $name = $rowvalues['forename']; $options.="<option value=\"$name\">.$name."</option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/227162-working-with-select-boxes/#findComment-1171812 Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 So...add the if statement... Where is the selected name coming from? The one you want to make already selected? Quote Link to comment https://forums.phpfreaks.com/topic/227162-working-with-select-boxes/#findComment-1171813 Share on other sites More sharing options...
MatthewJ Posted February 9, 2011 Share Posted February 9, 2011 while ($rowvalues=mssql_fetch_array($getnames)){ $name = $rowvalues['forename']; if($valuetocheck == $name) { $options.="<option value=\"$name\" selected=\"selected\">".$name."</option>"; } else { $options.="<option value=\"$name\">.$name."</option>"; } } Something like that... PS... you were missing a double quote, that is the reason for the edit. Quote Link to comment https://forums.phpfreaks.com/topic/227162-working-with-select-boxes/#findComment-1171907 Share on other sites More sharing options...
Psycho Posted February 9, 2011 Share Posted February 9, 2011 A slight modification to MatthewJ's code that is slightly better IMHO because you only have one statement to create the OPTION tag. By reducing the "branches" in the logic you get greater consistency and less bugs in your code. This assumes that $selectedValue for the record is already set. while ($rowvalues=mssql_fetch_array($getnames)) { $name = $rowvalues['forename']; $selected = ($name==$selectedValue) ? ' selected="selected"' : ''; $options.="<option value=\"{$name}\"{$selected}>{$name}"</option>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/227162-working-with-select-boxes/#findComment-1171908 Share on other sites More sharing options...
PaulRyan Posted February 9, 2011 Share Posted February 9, 2011 Yeah I agree with that example Mjdamato, but did you know using ternary operators uses upto 1/3 more memory that a normal if/else statement? Something to consider, depending on how much usage your site will get ect Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/227162-working-with-select-boxes/#findComment-1171909 Share on other sites More sharing options...
Psycho Posted February 9, 2011 Share Posted February 9, 2011 ... did you know using ternary operators uses upto 1/3 more memory that a normal if/else statement? Well "up to" is a loaded statement. I did some tests and found no more than 10%. But, you have to consider that is only 10% more that that one miniscule operation (were talking ~96 bytes per ternary vs. IF/ELSEin my tests). Granted you do need to consider that with respect to the traffic of your site. But, a sever *should* have billions of bytes available. There are other considerations as well. Not using a ternary operator will result in more verbose code that will increase the size of the php files on the physical disc and the time to read/parse those files. Again, that is a miniscule amount, but I'll use any excuse to keep my ternary operators. If I may use an expression: "You can tear my ternary operators from my cold, dead hands" Quote Link to comment https://forums.phpfreaks.com/topic/227162-working-with-select-boxes/#findComment-1171932 Share on other sites More sharing options...
MatthewJ Posted February 9, 2011 Share Posted February 9, 2011 As a side note... I would use the same solution as proposed by mjdamato I just didn't feel like typing much this morning so I threw the obvious at the OP. That's why I love this site, there is always someone to help clarify the answer of others can't make it back soon enough. Quote Link to comment https://forums.phpfreaks.com/topic/227162-working-with-select-boxes/#findComment-1171952 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.