[PHP-DB] Enum table entry

2002-10-19 Thread Shiloh Madsen
Have a question that im trying to figure out how to resolve. I have a field type in 
mysql that is of the enum type. Unless youre familiar with Dungeons and Dragons, you 
wont get what the values mean, but hopefully youll get the gist anyway. I have a 
column labelled school which holds an enum data type comprised of the values 1 through 
40. From the website front end, where the data is being entered, i want to display, 
ideally a series of checkboxes, otherwise a list which would allow a user to select 
multiple items in that will translate into this enum field. For instance, a series of 
checkboxes with items such as abjuration, conjuration, divination, and others, which 
will all have a numeric value which gets plugged into the enum field. for instance, if 
a user selected abjuration, and divination, it would be plugged into sql as 1, 3 (or 
however enum data is input into its column). That being the case how do i utilize php 
to get this to work? what kind of form elements etc... The problem im seeing with 
checkboxes are that they are discreet and dont group together, so i cant get all the 
data to go into one column in mysql. Hopefully i havent horribly confused the issue 
and some kind soul out there can tell me how to send this data across. As a double 
nice thing...how would you write it to pull the data back out...ie, convert 1, 3 to 
show abjuration, divination? Thanks for the help in advance. 



Re: [PHP-DB] Enum table entry

2002-10-19 Thread Rick Widmer
At 11:48 PM 10/18/02 -0500, Shiloh Madsen wrote:



 For instance, a series of checkboxes with items such as abjuration, 
conjuration, divination, and others, which will all have a numeric value 
which gets plugged into the enum field. for instance, if a user selected 
abjuration, and divination, it would be plugged into sql as 1, 3

Assuming your field is:

school enum( 'abjuration', 'conjuration', 'divination', ... ),

The database will return, 'abjuration,divination' in the example listed
above, and it will expect the same kind of string when setting the field
in an UPDATE or INSERT query.



(or however enum data is input into its column). That being the case how 
do i utilize php to get this to work? what kind of form elements etc... 
The problem im seeing with checkboxes are that they are discreet and dont 
group together, so i cant get all the data to go into one column in mysql. 
Hopefully i havent horribly confused the issue and some kind soul out 
there can tell me how to send this data across. As a double nice 
thing...how would you write it to pull the data back out...ie, convert 1, 
3 to show abjuration, divination? Thanks for the help in advance.



To get the data in/out of the database you can do something like this:


Start with an array of possible choices, because you are going to
have to act on each possible choice.

$SchoolChoices = array( 'Abjuration', 'Conjuration', 'Divination', ... );




To setup the variables from the table for display.  Note the value
from the database is in the string $School.


reset( $SchoolChoices );

while( list( , $Choice ) = each( $SchoolChoices )) {
   $VarName = 'School' . $Choice;
   $$VarName = ereg( $Choice, $School ) ? 'CHECKED' : '';
   }



Now you can send the form displayed below with the values from
the database.



FORM Method=GET  ...

INPUT Type=checkbox Name=SchoolAbjuration Value=?=$SchoolAbjuration?
INPUT Type=checkbox Name=SchoolConjuration Value=?=$SchoolConjuration?
INPUT Type=checkbox Name=SchoolDivination Value=?=$SchoolDivination?
...
/FORM

===


After the user enters the form, you can decode the fields and put the
data into a string for storage in the database with:


reset( $SchoolChoices );

$School = '';
while( list( , $Choice ) = each( $SchoolChoices )) {
   if( 'on' == $_get( School$Choice ))  {
  $School .= ',' . $Choice;
  }
   }

$School = substr( $School, 1 );


Now you can INSERT/UPDATE the database with $School to set the enum field.




You can create the checkbox fields from $SchoolChoices with the following:

reset( $SchoolChoices );

while( list( , $Choice ) = each( $SchoolChoices )) {
   $VarName = 'School' . $Choice;
   echo INPUT Type=\checkbox\ Name=\$VarName\ Value=\$$VarName\;
   }




For extra credit, figure out how you can create the $SchoolChoices array
from the output of the following query:

   DESCRIBE TableName School;

(Yes you can send this to mysql_query, and get the possible values of the 
enum.)



Rick  


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php