Jump to content

enum output to a "select" value? Oo?


Recommended Posts

Well i think the title should say it all to be honest, i have a database with a table and a coble of rows in there. There is one of the rows that has "ENUM" as type and this "Højre" is default on the "ENUM" and has following options in it "Højre, Venstre".


Now my question is, what shall i do to make this enum work in a "<select>" html?


This is my code atm:

echo "<select name='info_side'>";	echo "<option value='0'>Vælg Genre</option>";	$info_side_query = "SELECT dsg_id_runner, dsg_info_side FROM dataskema_generator WHERE dsg_id_runner = $caller";	$info_side_result = $mysqli->query($info_side_query);	while ($row = $info_side_result->fetch_assoc())	{		echo "<option value='" .$row['dsg_info_side']."'>" .$row['dsg_info_side']."</option>";	}echo "</select>";

And yes i know the value in there should be numeric, but the insert HAS to be a "Højre, Venstre" string? Right Oo?


Hoping an answer soon ^^'

Thanks! :D

Edited by rootKID
Link to comment
Share on other sites

  • 7 years later...

I searched and was hoping to find the answer to this question myself. Perhaps the OP has solved his own problem.  I have the same question. I have a table with an enum field named device with set values of 'Computer', 'Laptop', 'Smartphone' etc so how do I create a dropdown selection input list in an HTML to display the options and a previously selected (stored in MySQL) option in that list?

Edited by adam12
Link to comment
Share on other sites

Usually, since the enum field has a small number of options and they never change, you just need to hardcode the options instead of trying to get a list of all of them from the database.

To select an option in a dropdown based on the data from a database query, you just need to check whether the value from the database is equal to the value of the dropdown option.

$row = query->fetch(); // Code to get a database row from a query
$options = [ 'Computer', 'Laptop', 'Smartphone' ];

<select name="device">
  foreach($option as $option) {

    // Select the option if the row matches it
    // I'm assuming your database has a field named "device" which stores the ENUM value.
    $selected = '';
    if($option == $row['device']) {
      $selected = ' selected'

    // Write the HTML code for the option
    echo "<option value='{$option}' {$selected}> {$option} </option>";


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...