Set default SELECT option from pre-built list

I have a drop down list that is populated by a query against a database (run from a function since this list is used in multiple locations in the website. I’m curious if there is a way to choose an option as the default out of that list after the fact?

Basically my function returns a variable that is a list similar to below that I wrap in <option></option> brackets.

<option value=“None”>None</option>
<option value=“206”>ABC</option>
<option value=“95”>DEF</option>
<option value=“212”>GHI</option>
<option value=“188”>JKL</option>
<option value=“197”>MNO</option>

When the page loads it has another query that returns a value that corresponds to one of the option values from the above list. I would like THAT option to then be selected option so the end user can see what the original value was, but can choose a different one if they are editing this record.

Is there a way to dynamically add the selected value to the results returned in the variable? Or should I just run an additional query against the database the would retrieve the ABC, DEF, GHI, etc as appropriate based on the value returned from the main query?

I realize I kind of went a bit around and around in describing this, hopefully you get the general idea and you have some suggestions on the best way to address this.

Greg

If the function returns the complete options list as a string that you only have to display, then the solution could be to change the function: add a parameter (the value to be selected) and in the loop in the function add the mechanism that checks if the current value is the to be selected value, and act accordingly.

If the function returns an array with values, and the creation of the actual <option> tags is done outside the function, then do the checking against the to be selected value in that part of the code.

Create a function which is passed the array of values and the setting which should be preselected:


// spoofing your array collected from your db

$arr = array(212=>"GHI", 95=> "DEF");

// the one to preselect to
$chosen = 95 ;

function createSelectList($name, $arr, $chosen_one) {
 $html = "<select name='$name'>" . PHP_EOL ;

 foreach($arr as $key=>$val){

 $selected = ($key === $chosen_one) ? "selected='selected'" : "" ;

 $html .= "<option value='$key' $selected>$val</option>" . PHP_EOL ;

}

return $html . "</select>" .PHP_EOL ;
}

echo createSelectList("mylist", $arr, 95);

//<select name='mylist'>
//<option value='212' >GHI</option>
//<option value='95' selected='selected'>DEF</option>
//</select>

You can take this a lot further and build in the sql statement as well, if you were inclined - though some might say this function is then taking on too much responsibility – but you can make it quite a useful tool.

See? That’s why I visit the forums. Just as I was feeling good about myself for finally utilizing functions in my app I get reminded that you can make functions do MUCH more of your bidding if you just think about it for a minute and actually utilize their basic features.

Thanks Guys!!! Going to rewrite the function.

Greg

Just rereading over your code suggestion and I realized you used an array to store the returned database values. I currently have my function iterating through the results and building the <option></option> lines for my select statement. Would it be better to iterate that into an array and then use that array? Maybe even keep the array as a live (session?) variable that can be use at different times versus making another query to the DB?

I do a process like this in a few places throughout the app, some of them are fairly unlikely to be modified by another user that would require a live update. Would it be better to keep these database results as arrays stored in variables rather than making queries at different times? I had considered running these queries and rather than creating a variable storing the results in a flat file that would just be “included” where necessary throughout the app?

I realize this question has kind of veered off my original one, but this is where my train of thought has taken me. (-:

Greg