Re: mysql's enum type used with php/html form?
| From: | Cynic | Date: | Wed, 24 May 2000 13:33:13 +0000 |
| Subject: | Re: mysql's enum type used with php/html form? | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-77@lists.php.net to get a copy of this message | ||
At 08:30 24.5.2000 -0500, Kenn Murrah wrote the following:
--------------------------------------------------------------
>Good morning.
>
>I'm having a bit of trouble using a form with
>pull-down select menus, then writing the choice
>to a mySQL field with a type of "enum."
>
>Can anyone point me in the direction of a simple
>example of this? Once I see the code, I'm sure
>I'll slap myself on the forehead, berate myself for
>not seeing the obvious, and promise myself that
>I'll never ask anything this elementary on the list
>again.
>
>In the meantime, though, I could use any advice
>that anyone might have.
>
>Thanks,
>
>Kenn Murrah
------end of quote------
This was asked yesterday, IIRC. Look below for answers.
>From: Simon Robson <simon@mangorains.com>
>To: php-general@lists.php.net
>Subject: Re: [PHP-GENERAL] Displaying enum field values
>
>
> > 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
>From: fernando.silva@netzero.pt
>To: M.vanderMerwe@expro.shell.co.uk, php-general@lists.php.net
>Subject: Re: [PHP-GENERAL] Displaying enum field values
>
>
> >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?
>
>
>Oi!
>I use something like this:
>
>
>$sql = "SHOW FIELDS FROM $table";
>$res = mysql_query($sql);
>for($i = 0; $i < count($res); $i++) {
> if($res[$i]['Field'] == $field)
> break;
>}
>
>
>$enum = str_replace("enum(", "", $res[$i]['Type']);
>
>
>$enum = ereg_replace("\)$", "", $enum);
>$enum = str_replace("'", "", $enum);
>$enum = explode(",", $enum);
>echo "<SELECT name=$name>";
>for($i = 0; $i < count($enum); $j++)
> echo '<OPTION value="'.$enum[$j].'">'.$enum[$j];
>echo '</SELECT>';
>
>
>I hope this help,
> Fernando Silva
____________________________________________________________
Cynic:
A member of a group of ancient Greek philosophers who taught
that virtue constitutes happiness and that self control is
the essential part of virtue.
cynic@mail.cz
ICQ: 2808 2505