Connecting Tech Pros Worldwide Help | Site Map

Populating form item from SQL enum set

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 09:21 AM
Pjotr Wedersteers
Guest
 
Posts: n/a
Default Populating form item from SQL enum set

Hi

I am not even sure if it is at all possible, and I can't find the stuff I am
looking for, probably using the wrong search keys. WAMP machine (XP,
2.50.2, 4.1.5, 5.0.2)

I have a field in the MySQL table 'members':
"membertype" enum ('adult','student','senior','child','honour')

Sofar the form used for the db only has text fields, and the data entry user
relies on IE's autocomplete to always select a valid entry for the field. I
quick fixed this script so it at least checks the validity.

I'd prefer to have a general solution where the input field is a dropdown
box or something (or radio buttons) populated directly with all valid enum
fields. In case the db later is altered, enum set is modified, all the forms
should change automatically.

(How) can I retrieve the enum values from the db and use them in a dropdown
? Or who has a pointer to the right place where I can find how to do this ?
Or an example simlar snippet maybe ?
THANKS!

Pjotr



  #2  
Old July 17th, 2005, 09:21 AM
brommer
Guest
 
Posts: n/a
Default Re: Populating form item from SQL enum set

I've used this once, but it's a bit ugly so here's to hoping a better
solution comes along :)

<?php

$sql = "SHOW COLUMNS FROM members LIKE 'membertype'";
$qry = mysql_query($sql);
$res = mysql_fetch_object($qry);
// This returns a row with a field 'Type' containing 'enum(...)'

$res->Type = str_replace('enum', 'array', $res->Type);

eval(" \$memberTypes = $res->Type; ");


foreach($memberTypes as $type) {
echo "<option...etc."
}


?>


Pjotr Wedersteers wrote:[color=blue]
> Hi
>
> I am not even sure if it is at all possible, and I can't find the stuff I am
> looking for, probably using the wrong search keys. WAMP machine (XP,
> 2.50.2, 4.1.5, 5.0.2)
>
> I have a field in the MySQL table 'members':
> "membertype" enum ('adult','student','senior','child','honour')
>
> Sofar the form used for the db only has text fields, and the data entry user
> relies on IE's autocomplete to always select a valid entry for the field. I
> quick fixed this script so it at least checks the validity.
>
> I'd prefer to have a general solution where the input field is a dropdown
> box or something (or radio buttons) populated directly with all valid enum
> fields. In case the db later is altered, enum set is modified, all the forms
> should change automatically.
>
> (How) can I retrieve the enum values from the db and use them in a dropdown
> ? Or who has a pointer to the right place where I can find how to do this ?
> Or an example simlar snippet maybe ?
> THANKS!
>
> Pjotr
>
>[/color]
  #3  
Old July 17th, 2005, 09:21 AM
Andy Hassall
Guest
 
Posts: n/a
Default Re: Populating form item from SQL enum set

On Fri, 29 Oct 2004 18:31:07 +0200, "Pjotr Wedersteers" <pjotr@wedersteers.com>
wrote:
[color=blue]
>I am not even sure if it is at all possible, and I can't find the stuff I am
>looking for, probably using the wrong search keys. WAMP machine (XP,
>2.50.2, 4.1.5, 5.0.2)
>
>I have a field in the MySQL table 'members':
>"membertype" enum ('adult','student','senior','child','honour')
>
>Sofar the form used for the db only has text fields, and the data entry user
>relies on IE's autocomplete to always select a valid entry for the field. I
>quick fixed this script so it at least checks the validity.
>
>I'd prefer to have a general solution where the input field is a dropdown
>box or something (or radio buttons) populated directly with all valid enum
>fields. In case the db later is altered, enum set is modified, all the forms
>should change automatically.
>
>(How) can I retrieve the enum values from the db and use them in a dropdown
>? Or who has a pointer to the right place where I can find how to do this ?
>Or an example simlar snippet maybe ?
>THANKS![/color]

http://groups.google.com/groups?selm...&output=gplain

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
  #4  
Old July 17th, 2005, 09:21 AM
Pjotr Wedersteers
Guest
 
Posts: n/a
Default Re: Populating form item from SQL enum set

brommer wrote:[color=blue]
> I've used this once, but it's a bit ugly so here's to hoping a better
> solution comes along :)
>
> <?php
>
> $sql = "SHOW COLUMNS FROM members LIKE 'membertype'";
> $qry = mysql_query($sql);
> $res = mysql_fetch_object($qry);
> // This returns a row with a field 'Type' containing 'enum(...)'
>
> $res->Type = str_replace('enum', 'array', $res->Type);
>
> eval(" \$memberTypes = $res->Type; ");
>
>
> foreach($memberTypes as $type) {
> echo "<option...etc."
> }
>
>[color=green]
>>[/color][/color]
TY, it works, and indeed doesn't look very elegant, but hey, it will do. The
other hint (by andy) has the same principle. I'll dive into the MySQL stuff
another time to see if I can find some more useful stuff on this matter.

again Thanks, both!


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.