Jump to content

Working with Select Boxes


leachus2002

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

... 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"

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.