PHP/MySQL form dropdown/select list

Hi all,
I have a PHP/MySQL form where people can update their profile. After the user has registered and logged in to update their profile the form is populated with the data you have previously submitted. Something like:


<input size="25" maxlength="255" name="city" type="text" <?php if ($city!='') { echo 'value="' . $city . '"'; }?> />

I’m have trouble finding a clean way to do this with dropdown/select lists since they don’t have a ‘value’ attribute. I had a series of IF/ELSE statements to upload their submitted value first in the list but it seemed a bit much. Anything better out there?

Thanks!

what is it you are trying to do?

why did you use an if statement instead of this?


<input size="25" maxlength="255" name="city" type="text" value="<?php echo $city; ?>" />

an empty value attribute is the same as omitting the attribute.

I guess I was just trying the keep things clean - didn’t want to throw the value attribute out there if it wasn’t needed.

The problem is with dropdowns. I need to load the default value pulled from the database. So…


<select name="title">
        <option value="Designer">Designer</option>
        <option value="Programmer">Programmer</option>
        <option value="Writer">Writer</option>
</select>

So if this person is a programmer (not a designer) I need to display that option first so they don’t have to reselect their title everytime they upate their profile. I was doing some like:


<select name="title">
<?php
if ($title=='Designer') {
echo '<option value="Designer">Designer</option>
        <option value="Programmer">Programmer</option>
        <option value="Writer">Writer</option>';
}
if ($title=='Programmer') {
echo '<option value="Programmer">Programmer</option>
        <option value="Designer">Designer</option>
        <option value="Writer">Writer</option>';
}
if ($title=='Writer') {
echo '<option value="Writer">Writer</option>
        <option value="Programmer">Programmer</option>
        <option value="Designer">Designer</option>';
}
?>
</select>

Is there a better way to do this? Some of my select box have quite a few options and it would be a lot of code.

I would do something like this:




<option value="Designer" <?php if ($title=='Designer') { echo "SELECTED"; } ?>>Designer</option>

<option value="Programmer" <?php if ($title=='Programmer') { echo "SELECTED"; } ?>>Programmer</option>

<option value="Writer" <?php if ($title=='Writer') { echo "SELECTED"; } ?>>Writer</option>

Ras

<option> elements have a special attribute named selected.

the element with this attribute will be preselected for the user, regardless of the order of the list.

heres a function i use to make select menus. it allows you to specify different text for the option than what the value is, although most of the time you will probably want them to be the same.


function select_menu($name, $options, $selected) {
    $list = '';
    foreach ($options as $value => $text) {
        $is_selected = $value == $selected ? ' selected="selected"' : '';
        $list .= "  <option value=\\"$value\\"$is_selected>$text</option>\
";
    }
    return "<select name=\\"$name\\" id=\\"$name\\">\
$list</select>\
";
}


// usage

$options = array(
    'value for this option' => 'text displayed for this option',
    'Writer' => 'Writer',
    'Programmer' => 'Programmer',
    'Designer' => 'Designer'
);

$menu_name = 'title';
// you could grab selected_val from db
$selected_value = 'Designer';

echo select_menu($menu_name, $options, $selected_value);

edit, i was a bit late :smiley:

D’oh! So simple of a soluntion I missed it.

Thanks all!

Hey I just wanted to to say thank you for the tip how to created selected values in a drop down list using a value in the mysql database.

However, it seems to me that there should be a better way to go about this so that you use the power of the database. If I have a table of values couldn’t I somehow use that table to provide values for the drop down list. As opposed to having to include them as options in my html code? I am very new at this but love the idea of allowing the mysql database to dynamically update a webpage. If I make an addition to the table holding the values it would be available to all my webpages using dropdown lists based upon that table. Is this possible or am I dreaming???

Thanks


$options = array();
while ($row = mysql_fetch_assoc($result)) {
    $options[$row['car']] = $row['car'];
}

$selected = 'whatever value you want preselected';

echo select_menu('cars', $options, $selected);

that function isnt ideal of all uses. but the logic being used is what you should be looking at.

Clamcrusher thanks for the advice. I had to fiddle with it a little to fit my purposes and as I am new to programming entirely it took some trial and error but this is what I got to work for me. I have a form that has multiple dropdown lists populated by a project table in a mysqldatabase. Each dropdown list is populated by a separate table in the db and the value for that specific field in the project table is selected by default. Here is an example code for one dropdown:

Software :
<!-- name of this dropdown field is "Software" -->
<?php
// Get records in all columns from software_tbl.
$softwareresults=mysql_query("SELECT * FROM software_tbl ORDER BY Software");

$options = array();
while ($softwarerow = mysql_fetch_assoc($softwareresults)) {
    $options[$softwarerow['Software']] = $softwarerow['Software'];
}

$selected = $row['Software'];

echo select_menu('Software', $options, $selected);

 ?> 

The $row variable gave me the data for the specific project I was dealing with and the form is filled with the values currently in the db for that project.

Thanks again this thread has been very helpful for me.