JHamill
JHamill

Reputation: 145

Default value in drop down

I am creating a edit user page which contains text boxes and drop down menus populated from data within the database.

I currently call a function to populate the drop down menus. For example, here is the code that I used to populate a drop down:

<select name="manager">;
<?php
// printing the list box select command
foreach($managerDetails as $row){
//Array or records stored in $managerDetails
echo "<option value='${row['userID']}'>
${row['lastName']}, ${row['firstName']}
</option>";
/* Option values are added by looping through the array */
}
?>
</select>

I am attempting to have the combo box automatically display the value from the database. I have attempted placing this as the value of :

value="<?php echo $userDetails['managerID']; ?>"

Thanks.

Upvotes: 0

Views: 12049

Answers (3)

Stopeck
Stopeck

Reputation: 1

<select name="manager">
    <?php
        $user_id = //the current user id selected from the database/or can get it through a login SESSION['']
        foreach($managerdetails as $row=>$d){ ?>
            <option <?php if($d == $user_id)echo "selected";?> value='${row['userID']}'><?php echo ${row['lastName']}, ${row['firstName']}?></option>
        <?php } ?>
</select>

Upvotes: -1

A.R.
A.R.

Reputation: 15704

You want to say:

<option selected="selected">DefaultValue</Option>

here is a great example:
http://www.tizag.com/htmlT/htmlselect.php

Upvotes: 2

konsolenfreddy
konsolenfreddy

Reputation: 9671

echo "<option value='${row['userID']}' ". (($userDetails['managerID'] == $row['userID']) ? "selected='selected'":"").">${row['lastName']}, ${row['firstName']}</option>";

will do

Upvotes: 5

Related Questions