3

So I created a function that will pull the values from an enum field in my database:

<?php
function set_and_enum_values( &$conn, $table , $field )
{
    $query = "SHOW COLUMNS FROM `$table` LIKE '$field'";
    $result = mysqli_query( $conn, $query ) or die( 'Error getting Enum/Set field ' . mysqli_error() );
    $row = mysqli_fetch_row($result);

    if(stripos($row[1], 'enum') !== false || stripos($row[1], 'set') !== false)
    {
        $values = str_ireplace(array('enum(', 'set('), '', trim($row[1], ')'));
        $values = explode(',', $values);
        $values = array_map(function($str) { return trim($str, '\'"'); }, $values);
    }

    return $values;
}
?>

I am executing this function in a HTML form, and it's working like a charm to pull the values and list them in a drop down. Here is what I'm using:

<td><select name="owner">
<?php
  $options = set_and_enum_values($con, 'inventory', 'owner');
  foreach($options as $option):
     $selected = (isset($row['owner']) && $row['owner'] == $option) ? ' selected="selected"' : '';
?>
    <option><?php echo $selected; ?><?php echo $option ?></option>
<?php endforeach; ?>
</select></td>

However, I am unable to figure out how to make the default (aka, SELECTED) value show as the default in the drop down. What I have right now will rename the current used value selected=<value1>, but it doesn't make it the default. Can anyone assist? Does my question make sense?

Thanks!

EDIT: Here is the working code, now that I've fixed my syntax error:

<td><select name="owner">
<?php
  $options = set_and_enum_values($con, 'inventory', 'owner');
  foreach($options as $option):
     $selected = (isset($row['owner']) && $row['owner'] == $option) ? ' selected="selected"' : '';
?>
    <option<?php echo $selected; ?>><?php echo $option ?></option>
<?php endforeach; ?>
</select></td>
dhorn
  • 63
  • 8
  • http://stackoverflow.com/questions/3518002/how-to-set-default-value-for-html-select-element – jesusnoseq Dec 31 '13 at 21:54
  • Seems I made a mistake with my – dhorn Dec 31 '13 at 22:03

2 Answers2

1
< option value="< ?php echo $selected ?>">< ?php echo $selected ?>< /option>
Mihai
  • 26,325
  • 7
  • 66
  • 81
les
  • 564
  • 7
  • 19
  • BTW there are extra spaces because the php tags caused parts of my answer to disappear without them. – les Dec 31 '13 at 22:09
  • Thanks. I had a typo in where my option tags were that messed it up. My text editor is highlighting it as if I left tags open, but that's not the case. Thanks for your response! – dhorn Dec 31 '13 at 22:22
1

To select one ore more items in the select element you only have to put the "selected" inside the element like:

<select id="unittype" name="unittype">
  <option value="1"> Miner </option>
  <option value="2"> Puffer </option>
  <option value="3" selected> Snipey </option>
  <option value="4"> Max </option>
  <option value="5"> Firebot </option>
</select>

here the Snipey (value=3) is selected.

So in your example it have to be inside the ... maybe it was only a typo:

 <option <?php echo $selected; ?>><?php echo $option ?></option>

Tom, oe1tkt

Tom Kuschel
  • 695
  • 8
  • 19