with - php select option

How do I set the selected item in a drop down box (6)

Is there any way to set the selected item in a drop down box using the following 'type' code?

<select selected="<?php print($row[month]); ?>"><option value="Janurary">January</option><option value="February">February</option><option value="March">March</option><option value="April">April</option></select>

The database holds a month.. and I want to allow on the edit page, them to choose this month.. but it to be pre-filled with their current setting?

A Simple Solution: It work's for me

<div class="form-group">
    <label for="mcategory">Select Category</label>
    <select class="form-control" id="mcategory" name="mcategory" required>
        <option value="">Please select category</option>
        <?php foreach ($result_cat as $result): ?>
        <option value="<?php echo $result['name'];?>"<?php 
               echo 'selected';
           } ?>><?php echo $result['name']; ?></option>
        <?php endforeach; ?>

Its too old but I have to add my way as well :) because it is generic and useful especially when you are using static dropdown values.

function selectdCheck($value1,$value2)
     if ($value1 == $value2) 
      echo 'selected="selected"';
     } else 
       echo '';

and in you dropdown options you can use this function like this and you can use this as many as you can because it fits with all of your select boxes/dropdowns

<option <?php selectdCheck($row[month],january); ?> value="january">january</option>

:) I hope this function help others

Simple way

<select class ="dropdownstyle" name="category" selected="<?php print($messageeditdetails[0]['category_id']); ?>">

<option value=""><?php echo "Select"; ?></option>

<?php  foreach ($dropdowndetails as $dropdowndetails) { ?>
    <option <?php if($messageeditdetails[0]['category_id'] == $dropdowndetails['id']) { ?> selected="<?php echo $dropdowndetails['id']; ?>" <?php } ?> value="<?php echo $dropdowndetails['id']; ?>"><?php echo $dropdowndetails['category_name']; ?></option>
<?php } ?>

This is the solution that I came up with...

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">   
    <select name="select_month">
            if (isset($_POST['select_month'])) {
                if($_POST["select_month"] == "January"){
                    echo '<option value="January" selected="selected">January</option><option value="February">February</option>';
                elseif($_POST["select_month"] == "February"){
                    echo '<option value="January">January</option><option value="February" selected="selected">February</option>';
                echo '<option value="January">January</option><option value="February">February</option>';
    <input name="submit_button" type="submit" value="Search Month">

You can use this method if you use a MySQL database:

$result = mysql_query("SELECT * FROM users WHERE `id`!='".$user_id."'");
while ($row = mysql_fetch_array($result))
    if ($_GET['to'] == $row['id'])
        $selected = 'selected="selected"';
    $selected = '';
    echo('<option value="'.$row['id'].' '.$selected.'">'.$row['username'].' ('.$row['fname'].' '.substr($row['lname'],0,1).'.)</option>');

It will compare if the user in $_GET['to'] is the same as $row['id'] in table, if yes, the $selected will be created. This was for a private messaging system...

You mark the selected item on the <option> tag, not the <select> tag.

So your code should read something like this:

    <option value="January"<?php if ($row[month] == 'January') echo ' selected="selected"'; ?>>January</option>
    <option value="February"<?php if ($row[month] == 'February') echo ' selected="selected"'; ?>>February</option>
    <option value="December"<?php if ($row[month] == 'December') echo ' selected="selected"'; ?>>December</option>

You can make this less repetitive by putting all the month names in an array and using a basic foreach over them.