jeff5656 Posted June 17, 2008 Share Posted June 17, 2008 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... Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 17, 2008 Share Posted June 17, 2008 use isset() to check if the checkbox has been checked, usually stored at $_POST['var_name']. if it is, then just get the value at $_POST['var_name'] as a parameter for the query. Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted June 17, 2008 Author Share Posted June 17, 2008 But how do I structure the database table itself? Each field can only store one value, but what if there are 3 values? Where do all 3 get stored in the table? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted June 17, 2008 Share Posted June 17, 2008 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. Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 17, 2008 Share Posted June 17, 2008 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... Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted June 17, 2008 Author Share Posted June 17, 2008 so if smith has 3 procedures and johnson has 1, how do I store all 3 to go with johnson under the same variable name "procedures"? Quote Link to comment Share on other sites More sharing options...
Wolphie Posted June 17, 2008 Share Posted June 17, 2008 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 ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.