Jump to content

check all that apply


jeff5656

Recommended Posts

I'm have a bit of a brain fart - and I'm a novice:

 

If I have a field called "procedure type" and in the form I have a "check all that apply", how do I store that in the database?  If someone checks 3 different procedures, where shouild these be stored? I can't have one field called procedure_type, so do I need a separate field for each one?  That can't be right...

Link to comment
Share on other sites

Use a one-to-many relationship...

 

An example:

 

users
-----
id
name

phone_numbers
-------------
id
user_id
number

 

Each user is listed once.  For each phone number the user has, an entry is put into the phone_numbers table that has the user_id and the phone number.

 

Apply the same principle to what you are trying to accomplish.

Link to comment
Share on other sites

option 1:

create a table for all the options selected, of course you can create a separate table for the options, then store the interconnection there, from the record to be stored from the selection and the option selected.

 

option 2:

store it as a delimited string. you just need to explode() it.

 

there are other options but i think this two would do... :)

Link to comment
Share on other sites

In order to make them into an easy to use string you would have to make the checkboxes an array.

 

<input type="checkbox" name="procedures[]" value="Value 1" />
<input type="checkbox" name="procedures[]" value="Value 2" />

 

Then for the PHP

 

<?php
if(isset($_POST['procedures'])) {
  $procedures = implode(', ', $_POST['procedures']);
  echo $procedures; // Output if both were checked: Value 1, Value 2
}
?>

 

This can then be stored in one database field. And when calling them you could display them however you liked by exploding them again into an array.

 

<?php
$procedure = explode(', ', $row['procedure']);
echo '<p>' . $procedure[0] . '</p>'; // Value 1
echo '<p>' . $procedure[1] . '</p>'; // Value 2
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.