如何动态选择复选框

I know this is a basic question that I should know the answer to, but i have never had to even consider it before and I could not find anything through google.

I want to automatically select or leave unselected checkboxes based on information in the database.

for example

I have two pairs of checkboxes each with a mens shoes and a womens shoes option. The user is able to select if they want only mens shoes or only womens shoes or both and the check boxes will be checked accordingly. Then there is another set for jeans which offer the same options.

In the database I have a few columns

id | foreignKeyId | Shoes | Jeans

The id column is auto incremented, the foreignKeyId is populated with the relevant foreignKey, shoes and jeans have been populated with an ENUM '0', '1', '2', '3'

0 = no selection

1 = mens selected

2 = womens selected

3 = both selected

I have then pulled the data from the database but I cant work out how to apply the information. I am assuming that I need to redesign my database.

What would be the best way to store and apply this information?

If you want to keep this structure in the db then:

after querying your db get into a variable the Jeans column's value.

$jeans = $row["Jeans"];

and then find out what you have to display to the user

$mens='';
$womens= '';

switch($jeans){
case "1": $mens=' checked="checked"'; break;
case "2": $womens=' checked="checked"'; break;
case "3": $mens=' checked="checked"'; $womens=' checked="checked"'; break;
}

echo "<input type='checkbox' name='mens' ".$mens." /> Mens";
echo "<input type='checkbox' name='womens' ".$womens." /> Womens";

I want to automatically select or leave unselected checkboxes based on information in the database.

You can try this logic:

Get the rows from the table then iterate. on each row, ask if the field's value is equal to the checkbox's value. If it is, set the $checked variable's value to "checked=true". If it is not, set the $checked variable's value to "" or an empty string. Echo the $checked variable in the <input type="checkbox"> tag.

Like this:

if($row->value == $checkboxvalue){
   $checked = "checked=true"
}else{
   $checked = "";
}

<input type="checkbox" <?php echo $checked?>>

I hope this helps you. :)

-JCB