douhuzhi0907 2014-02-13 16:28
浏览 25
已采纳

too long

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?

  • 写回答

2条回答 默认 最新

  • douchui3933 2014-02-13 16:41
    关注

    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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序