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
https://forums.phpfreaks.com/topic/110509-check-all-that-apply/
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
https://forums.phpfreaks.com/topic/110509-check-all-that-apply/#findComment-566933
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
https://forums.phpfreaks.com/topic/110509-check-all-that-apply/#findComment-566939
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
https://forums.phpfreaks.com/topic/110509-check-all-that-apply/#findComment-566962
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.