Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm fairly new to PHP, so please bare with me...

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??

Any help will be much appreciated!

Thanks, Patrick

share|improve this question
    
Why are you using print_r() for comparisons? You can compare two arrays $array_1 === $array_2 just as well, no? –  amsross Feb 13 at 16:35
add comment

2 Answers

up vote 0 down vote accepted

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.
share|improve this answer
    
Hi Nico, thanks for your speedy reply. I will alter and let you know... Do you think you could suggest anything another approach for me to research? Many Thanks! –  user3306818 Feb 13 at 17:32
    
@user3306818 see my edit –  Nico Feb 13 at 17:55
    
Thank you so much. It seems to make sense to me. I will give it a try! Thanks again! –  user3306818 Feb 13 at 18:04
add comment

Your array indices are off.

Add this above your if/else block: print_r($results);

You'll see that selecting choice and range-two outputs Array ( [0] => choice [2] => range-two ) and not Array ( [0] => choice [1] => range-two ), which is what $scen2 is equal to.

share|improve this answer
    
Thanks for this! Makes sense! –  user3306818 Feb 13 at 17:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.