GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - emikodes/PHP-Select-Options-MySQL-ENUM: Generates a "Select" type element, with options taken from an ENUM data type in MySQL.

BranchesTags
Open more actions menu

Latest commit

 

History

4 Commits
4 Commits

Folders and files

NameName
Last commit message
Last commit date
PHP_MySQLI_DropDown_Menu_ENUM.php
PHP_MySQLI_DropDown_Menu_ENUM.php
 
 
README.md
README.md
 
 

Repository files navigation

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

                $HTMLPage= <<<HTML
                    **//INSERT HERE THE HTML CODE YOU ONLY WANT TO SHOW IF THE DROPDOWN MENU HAS BEEN CORRECTLY INSERTED:**
                    <input type="submit" name="submit" value="Submit Form."/>
                    <input type="reset" value="Reset Form" />

                    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 "<h2>There was an error while creating the page.</h2>";
                    }else{
                        if($queryResult->num_rows == 0){
                            echo "<h2>Check the table definition.";
                        }else{
                            echo"<hr><br><h2>**Title**:</h2>";
                            $enumString = $queryResult->fetch_array()['Type'];
                            $enumString = substr($enumString,6,strlen($enumString)-8);

                            $enumString = str_replace('\'','',$enumString);
                            $arrayEnumOptions = explode(',',$enumString);
                            echo "<select name=\"**select_input_name**\" required>";
                            foreach($arrayEnumOptions as $singleOption){
                                echo "<option value=\"$singleOption\">$singleOption</option>";
                            }
                            echo "</select>";
                            echo $HTMLPage;
                        }
                    }

                }else{
                    echo "<h2>There was an error while creating the page.</h2>";
                }
            ?>

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!


About

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

Topics

enummysqlmysql-htmlmysql-selectphpphp-dynamic-html-select-mysqlphp-enum-dropdownphp-mysql-html-selectselectselect-enum

Resources

Readme
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.