An open API service indexing awesome lists of open source software.

https://github.com/emikodes/php-select-options-mysql-enum

Generates a "Select" type element, with options taken from an ENUM data type in MySQL.
https://github.com/emikodes/php-select-options-mysql-enum

enum mysql mysql-html mysql-select php php-dynamic-html-select-mysql php-enum-dropdown php-mysql-html-select select select-enum

Last synced: 17 days ago
JSON representation

Generates a "Select" type element, with options taken from an ENUM data type in MySQL.

Awesome Lists containing this project

README

          

# PHP “SELECT” Dropdown menu with MySQL ENUM:

---

> This code snippet written in **PHP**, generates a “**Select**” input type in **HTML**, by taking the allowed values from a **MySQL ENUM** data type.
>

---

## Code:

```php

HTML;

if($myDBObject = new mysqli("**SERVER_HOSTNAME**","**MySQL_USERNAME**","**MySQL_PASSWORD**")){

$myDBObject->select_db("DB_NAME");

if(!$queryResult = $myDBObject->query("SHOW COLUMNS FROM **tableName** WHERE Field=\"**columnName**\"")){
echo "

There was an error while creating the page.

";
}else{
if($queryResult->num_rows == 0){
echo "

Check the table definition.";
}else{
echo"


**Title**:

";
$enumString = $queryResult->fetch_array()['Type'];
$enumString = substr($enumString,6,strlen($enumString)-8);

$enumString = str_replace('\'','',$enumString);
$arrayEnumOptions = explode(',',$enumString);
echo "";
foreach($arrayEnumOptions as $singleOption){
echo "$singleOption";
}
echo "";
echo $HTMLPage;
}
}

}else{
echo "

There was an error while creating the page.

";
}
?>
```

---

## Technologies Used

- **PHP**
- **MySQLI**

---

## Usage

Just copy-paste my code snippet in your **.php** file, and modify the highlighted parts according to your needs.

You can insert **HTML** code in the “**$HTMLPage**” variable, to only show that code if the dropdown menu has been correctly generated.

For example, if you don’t want your form to be submittable if the dropdown menu wasn’t generated, insert the submit button code in that part.

---

## Contact

Coded by @emikodes - feel free to contact me!

---