Re: Displaying enum field values

From: Date: Tue, 23 May 2000 20:29:54 +0000
Subject: Re: Displaying enum field values
Groups: php.general 
Request: Send a blank email to php-general+get-391@lists.php.net to get a copy of this message
Here's what I use to make select boxes from an enum column ( I think it's been a while) :-) $result = mysql_query("SHOW COLUMNS FROM main LIKE 'subject'"); while ($answer = mysql_fetch_array($result)) { // Grab everything between the first and last single quote, exclusive ereg( "('(.*)')", $answer[1], $temp ); // Explode on the inner " ',' " combination $array = explode( "','", $temp[2] ); while (list ($key, $val) = each ($array)) { echo "<option value=\"$val\">$val</option>\n"; } } M@ --- Simon Robson <simon@mangorains.com> wrote: > > If I wanted to display the field values for an > ENUM type field in a > > form, how would I do this? Is there a tutorial > somewhere showing how > > it is to be done? > > > > I've played around with SHOW, but clearly I'm > doing something wrong by > > using this. > > $sql = "show columns from TABLENAME like > 'ENUM_COLUMN_NAME'"; > > is what the MySQL docs suggest, and it works, but it > returns a string of > the form > enum('option1','option2',etc...) > > Last time I had to do this I parsed the string out > (example below) -- but > I'd love to hear of a more elegant way to do this. > > $sql = "show columns from TABLENAME like > 'ENUM_COLUMN_NAME'"; > $conn = $this->CONN; > $result = mysql_query($sql,$conn); > $thisrow = mysql_fetch_array($result); > $valuestring = $thisrow[1]; > $valuestring = ereg_replace("enum", "", > $valuestring); > $valuestring = ereg_replace("\(", "", > $valuestring); > $valuestring = ereg_replace("\)", "", > $valuestring); > $valuestring = ereg_replace("\'", "", > $valuestring); > $values = split(",", $valuestring); > > which leaves $values as an array of the enum > options. Long winded > though... > > Simon > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: > php-general-unsubscribe@lists.php.net > For additional commands, e-mail: > php-general-help@lists.php.net > To contact the list administrators, e-mail: > php-list-admin@lists.php.net > __________________________________________________ Do You Yahoo!? Send instant messages & get email alerts with Yahoo! Messenger. http://im.yahoo.com/

« previous php.general (#391) next »