I have a page with a few checkboxes. You can make any combination of selections.
<form action="result.php" method="post">
<p>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="choice" id="choice">
option 1
</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="range-all" id="range-all">
option 2
</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="range-two" id="range-two">
option 3
</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="range-two" id="range-two">
option 4
</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="range-other-two" id="range-other-two">
option 5
</label>
<br>
</p>
<p>
<input type="submit">
</p>
</form>
Once you submit your selection, the form takes you to the next page where it compares the array of checkbox selections against a predefined array of options.
<?php
$options = array("choice", "range-all", "range-two", "range-other-two");
$checkboxes = $_POST['CheckboxGroup1'];
$intersection = array_intersect($options, $checkboxes);
$results = print_r($intersection, true);
//these are various scenarios based on what the user could choose
$scen1var = array("choice", "range-all");
$scen1 = print_r($scen1var, true);
$scen2var = array("choice", "range-two");
$scen2 = print_r($scen2var, true);
$scen3var = array("choice", "range-other-two");
$scen3 = print_r($scen3var, true);
$scen4var = array("range-all", "range-other-two");
$scen4 = print_r($scen4var, true);
if ($results === $scen1) {
echo "choice and range all";
}
elseif ($results === $scen2) {
echo "range consumables and range both";
}
//The elseif's carry on in this manner
else {
echo "something else";
}
?>
I am having an issue now where the first "if statement" works but the elseif doesn't. also, if I change the first "if statement" to compare $results to any other $scen ie. $scen2, $scen3, it doesn't work and just jumps to the "else" part.
I have a feeling I'm not making sense, so please let me know if I could explain a little more in detail...
Also this way I'm doing this seems a little overboard. Surely there is an easier way?
The problem is that array_intersect
does not preserve the key of the array.
<?php
$array1 = array(2, 4, 6, 8, 10, 12);
$array2 = array(1, 2, 3, 4, 5, 6);
var_dump(array_intersect($array1, $array2));
var_dump(array_intersect($array2, $array1));
?>
Yields the following:
array(3) {
[0]=> int(2)
[1]=> int(4)
[2]=> int(6)
}
array(3) {
[1]=> int(2)
[3]=> int(4)
[5]=> int(6)
}
You will have to replace
$scen1var = array("choice", "range-all");
$scen2var = array("choice", "range-two");
$scen3var = array("choice", "range-other-two");
$scen4var = array("range-all", "range-other-two");
for
$scen1var = array(1=>"choice", 2=>"range-all");
$scen2var = array(1=>"choice", 3=>"range-two");
$scen3var = array(1=>"choice", 4=>"range-other-two");
$scen4var = array(2=>"range-all", 4=>"range-other-two");
I strongly suggest you change your approach to this problem because this might not be as reliable as it seems..
This is my suggestion :
$checkboxes = $_POST['CheckboxGroup1'];
if(count($checkboxes) == 2 &&
in_array('choice', $checkboxes) &&
in_array('range-all', $checkboxes)
){
// choice and range only
}
// etc.
Of course, there is plenty of way to do this kind of stuff. Another approach.
$choice = false;
$range_all = false;
$range_two = false;
$range_other_two = false;
foreach($_POST['CheckboxGroup1'] as $checkbox)
{
if($checkbox == 'choice')
{
$choice = true;
}
elseif($checkbox == 'range_all')
{
$range_all = true;
}
//etc
}
if($choice && $range_all && !$range_two && !$range_other_two)
{
// choice and range_all only.
}
// etc.