1
votes

I have a list of checkboxes, and it is always growing, so for example..

<li><input type="checkbox" value="19384">Nutrition</li>
<li><input type="checkbox" value="22450">Weight Loss</li>
<li><input type="checkbox" value="98957">Fitness</li>
<li><input type="checkbox" value="34916">Lifestyle</li>

let's say I check Nutrition and Fitness, what is the best database schema/structure to save the checked ones so that I can retrieve it later?

The main problem is that these checkboxes are always increasing in number, so these four tomorrow can go up to 7 checkboxes or 10, and will always increase so I can't setup in database a specific amount of tables for each checkbox value.

I can't understand how I would upload the checked ones to database, and then retrieve them later?

Is it possible for me to upload the values to MySQL of each selected and then when retrieving them I'd have to check between the list and match with the ones that have been uploaded to MySQL and then if so use the checked="yes" in the input? I'm using PHP as well.

1
Store them as a string with a delimeter in betweenDarkBee
I wouldn't store them as delimiter separated list. I propose to use a separate table for those values.VMai

1 Answers

1
votes

The checkbox simply tells you if something is ... wait for it... checked or not. There is one possible value you would get from checking a box and that is that the checkbox has been checked. You will receive nothing if it isn't checked.

The first thing you need to do is make sure the checkbox's state can actually be checked. That means you need to name the checkboxes and don't put a value in them.

<li><input type="checkbox" name="nutrition">Nutrition</li>
<li><input type="checkbox" name="weight_loss">Weight Loss</li>
<li><input type="checkbox" name="fitness">Fitness</li>
<li><input type="checkbox" name="lifestyle">Lifestyle</li>

Now the php that will be checking to see if these are checked or not will look something like:

if (isset($_POST['nutrition']))
{
    //Code to process nutrition being checked
}

That's all - you do that for each one that you have.

As far as the database, since you think you'll be modifying the types, then you should create a separate table that relates someone checking one of these checkboxes to another record that has the related information.

For example, you have one table that shows a person:

  • person_id = auto-incremented integer as primary key
  • person_name = string/nvarchar/whatever

That's the person table. The next table would be your lookup table:

  • c_pk = autoincremented integer as primary key
  • c_checkbox_id = integer that relates to another table that stores the types of checkboxes you have
  • c_person_id = the id of the person the checkbox_id relates to

The last table would be the table that holds the type of checkboxes:

  • checkbox_id = autoincremented integer as primary key
  • checkbox_name = string representing name of checkbox (nutrition, weight loss, fitness et al)

That's how you lay it out so that you can add more at any time. You can also build your checkbox list dynamically by querying the table that holds all the checkbox names.

It's a bit much if you are just starting in programming and databases, but it is most definitely the right way to do it.