Reputation: 11
Hi i have the following problem:
$result1 = 'Produkt 1'; (contains in database: option1 = A, option2 = '')
$result2 = 'Produkt 2'; (contains in database: option1 = ABS, option2 = '1F')
$result3 = 'Produkt 3'; (contains in database: option1 = FP, option2 = 'F')
Now I have a formsearch with checkboxes for the options, and I want only the results that are checked:
<input name="asia" type="checkbox" id="asia" value="A" />
<input name="bio" type="checkbox" id="bio" value="B" />
<input name="break" type="checkbox" id="break" value="F" />
<input name="piz" type="checkbox" id="piz" value="P" />
<input name="south" type="checkbox" id="south" value="S" />
<input name="glut" type="checkbox" id="glut" value="1" />
<input name="lak" type="checkbox" id="lak" value="F" />
Pseudocode:
if ($_POST of all checkboxes == ''){
echo all results
}
if ($_POST['asia']!=''){
echo only results with A in option1
}
if ($_POST['asia']!='' && $POST['bio']!=''){
echo only results with A and B in option1
}
if ($_POST['break']!='' && $POST['piz']!='' && $POST['lak']!=''){
echo only results with F and P in option1 and F in option2
}
I need always the products that compare with all selected checkboxes.
I know that i can make for every possibility a if or elseif, but is there a faster way e.g. with arrays
Thanks a lot for every suggestion
Upvotes: 0
Views: 172
Reputation: 943516
The usual approach to this is to give all the checkboxes the same name (which should end in []
, e.g. name="name[]"
) and then treat $_POST['name']
as an array containing the values of all the checkboxes that are checked.
You can loop over it to deal with all the entries you have or use in_array
to see if a specific value is there.
If you need to deal with sets of data, then you can use:
<input type="checkbox" name="name[asia][]" value="foo">
<input type="checkbox" name="name[asia][]" value="bar">
And then $_POST['name']
will be an associative array containing arrays.
Upvotes: 2