Hi,

in some cases, you might prefer to use a multiple <SELECT> element; this can
give you a much more compact user interface (though you probably won't use
it if you have 40 options ...)

It goes like this:

              <SELECT multiple name="action[]" size="5">
                <OPTION value="0" selected="selected">--all--</OPTION>
                <OPTION value="1">conjuration</OPTION>
                <OPTION value="2">abjuration</OPTION>
                ...
                <OPTION value="n">divination</OPTION>
              </SELECT>

will pass an associative array $_POST['action'] ( "1" => "conjuration", ...,
"n" => "divination" )

Here I use the value "0" to select all - this calls a simple script to
generate the sequence "1,2, ...,n" that will be written into the DB. (In
general it is nice to provide a "all" checkbow when you have a long list.)

I use a set( '1','2', ..., 'n') MySQL datatype to store the thing. The data
appears physically as "1,2,...,n". Quite convenient. Probably a bit more
economical than storing a varchar (says the MySQL documentation ...)

HTH
Ignatius
____________________________________________
----- Original Message -----
From: "John Nichel" <[EMAIL PROTECTED]>
To: "Tom Rogers" <[EMAIL PROTECTED]>
Cc: "Shiloh Madsen" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>;
<[EMAIL PROTECTED]>
Sent: Saturday, October 19, 2002 9:15 AM
Subject: [PHP-DB] Re: [PHP] Enum table entry


> You know, I didn't even think about serializing the array.  I have a
> table set up for spells, one for schools, and a linking table using the
> spell id and the school id(s).  I could have done without the linking
> table.  Some of you people are pretty freakin' smart. :)
>
> One note though, when you pull the serialized data out, you may have to
> stripslashes before unserialize...
>
> $school = unserialize ( stripslashes ( $row['school'] ) );
>
> Tom Rogers wrote:
> > Hi,
> >
> > Saturday, October 19, 2002, 2:48:14 PM, you wrote:
> > SM> 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
> > SM> 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
> > SM> 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
> > SM> 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
> > SM> 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
> > SM> 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
> > SM> 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
> > SM> show abjuration, divination? Thanks for the help in advance.
> >
> >  I have never used enum type but I am sure it is not what you want as
> >  it will only store one item from a predefined list not a list of
> >  items. What you need to do is create an array of the selected items,
> >  serialize() it to store in the database in a varchar or mediumtext if
> >  it going to get big. Then when you read it back unserialize and loop
> >  through all the options setting "checked" if it is in the array.
> >  To keep the checkboxes grouped name them like this:
> >  <input type="checkbox" name="school[1]">
> >  <input type="checkbox" name="school[2]">
> >  <input type="checkbox" name="school[3]">
> >
> >  That will show up as an array under $_POST['school']
> >  so you can serialize it as is and store it.
> >
> >  <?
> >  if(isset($_POST['school'])) $school = serialize($_POST['school']);
> >  ?>
> >
> >  Playback is simple too
> >
> >   <?
> >   $school = unserialize($row['school']);
> >   for($i = 1;$i < 41;$i++){
> >          echo '<input type="checkbox" name="school['.$i.']"';
> >          if(isset($school[$i])) echo ' checked';
> >          echo '>';
> >   }
> >
> >   Un-checked boxes are not returned in the post and checked ones
> >   return Yes I think.
> >
> >
> >
>
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


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

Reply via email to