Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ENUM into a dropdown box

Status
Not open for further replies.

QAteste

Technical User
Jun 14, 2002
23
US
Is it possible using php to get the items from a ENUM type into a html dropdown box? If so, can you point me in the right direction?
 
There's likely a function for this, but here's one way...

Assume you have the following table "foo":

[tt]
Code:
+-------+--------------------------+------+-----+---------+----------------+
| Field | Type                     | Null | Key | Default | Extra          |
+-------+--------------------------+------+-----+---------+----------------+
| id    | tinyint unsigned         |      | PRI | NULL    | auto_increment |
| fu    | enum('No','Maybe','Yes') |      |     | No      |                |
+-------+--------------------------+------+-----+---------+----------------+
[/tt]

Then the following code:

[tt]
Code:
<?php

$dbh = mysql_connect ('localhost','test','test');

mysql_select_db ('test', $dbh);

$sql = &quot;DESCRIBE foo fu&quot;;

$result = mysql_query ($sql, $dbh);

$array = mysql_fetch_assoc ($result);

$enum_array = split (',',(preg_replace ('/enum\(|\)/', '', $array['Type'])));

?>
[/tt]

will initialize $enum_array to the values available in the enum field.
______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top