【发布时间】:2014-03-12 15:58:23
【问题描述】:
我有一个带有几个复选框的页面。您可以进行任何选择组合。
<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>
提交选择后,表单会将您带到下一页,它将复选框选择数组与预定义的选项数组进行比较。
<?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";
}
?>
我现在遇到第一个“if 语句”有效但 elseif 无效的问题。另外,如果我更改第一个“if 语句”以将 $results 与任何其他 $scen 进行比较,即。 $scen2,$scen3,不起作用,只是跳转到“else”部分。
我觉得我说不通,所以如果我能详细解释一下,请告诉我...
同样,我这样做似乎有点过火了。肯定有更简单的方法吗?
【问题讨论】:
-
您为什么使用
print_r()进行比较?你也可以比较两个数组$array_1 === $array_2,不是吗?
标签: php arrays forms checkbox comparison