From: JP H. <jp...@xs...> - 2002-05-14 23:31:07
|
Hi folks, I'm on a project that requires a couple of fields with checkboxes. No problem creating a field in the form (HTML). Done that dozens of times. BUT, never done it in MySQL. Please somebody tell me I don't have to have 18 individual fields (9 times 2 to cover the options of both fields) in order to do this. Searching the MySQL website for "checkbox" produced limited results. In fact the only appropriate hit was an excerpt from Kevin Yank's book (joke database example) but he's using a relational database design where the "categories" are in a separate table and not different values in a single field (like I'm accustomed to in PC based relational DB programs). I don't think I need to go relational with this project so I'd rather not follow this example. Then I searched my email archive and found this (which I don't truly understand): At 04:22 PM 1/15/2002 +0000, j8h9 wrote: >Storing Multiple Values in a Single MySql Table Column (Using Explode/Implode) > >Some applications may have a need to store many values or switches in a >database. Rather than creating numerous columns to hold all the unique >values, you can create one column to hold all the values. > >An example of this might be a form which uses multiple checkbox controls. >These are simple on/off switches. You can use the 0 and 1 values to >indicate whether the checkbox is set on or off. > >First, create a column in your MySql table (varchar 255 should be good). >Each unique value will be delimited by a colon (":"). So, the contents of >the "parameters" column might look like this: > >MySql table column called parameters contains: 0:1:1:0:0:0:1 > >Note the colon delimits each value. After selecting the column from the >database, you use the PHP explode verb to explode the values into an array: > >$ParmsX = explode(":",$row->parameters); > >The PHP documentation describes the explode verb thusly: "Returns an array >of strings, each of which is a substring of string formed by splitting it >on boundaries formed by the string separator." In the above example, the >colon is the delimiter. After the execution of the explode statement, the >ParmsX array contains seven elements (i.e. one for each of the zero/one >values above). > >When your finished with these parameters and are ready to update the >database, you can execute the implode verb. This verb is the opposite of >the explode verb and re-assembles the array elements back into one string >with the designated delimiter. > >$Parameters = implode(":",$ParmsX); > >Storing multiple values in a single MySql column is very efficient and >manageable. Make sure your variables are related. I use this method often >to store security bytes (switches) which control user access to specific >web pages or actions. > >Questions? Comments? Send me a note at tju...@ho... I created a field in the MySQL DB [ name = InfoRequested, type = varchar(255) ] but now I don't know how to write the PHP code to populate the field. Obviously there's an array involved in the field. Something like this? <input type="checkbox" name="InfoRequested[]" value="1"> Info Package 1 <input type="checkbox" name="InfoRequested[]" value="1"> Info Package 2 <input type="checkbox" name="InfoRequested[]" value="1"> Info Package 3 If that's proper then if only the first box is checked will it automatically know that $InfoRequested[1] and $InfoRequested[2] are "0"? If not then how do I make sure that happens? Do I make a conditional loop that says if ($InfoRequested[i] != "1") $InfoRequested[i] = "0"; ? I'd be grateful if someone could help me make sure I'm on the right track. Let's go ride, JP Honeywell |