需要一些帮助,通过计算响应复选框来解决问题。有一个10个问题的形式,每个问题都有6个复选框,可以选择多个选项。可能的答案A,B,C,D,E和F.有必要采取所有答复,并以A = 2,B = 4,C = 3,D = 1,E = 2,F =的形式汇总0
<p>
<label>1. QUESTION? </label><br>
<label><input type="checkbox" name="checkbox-one[]" value="А">А</label>
<label><input type="checkbox" name="checkbox-one[]" value="B">B</label>
<label><input type="checkbox" name="checkbox-one[]" value="C">C</label>
<label><input type="checkbox" name="checkbox-one[]" value="D">D</label>
<label><input type="checkbox" name="checkbox-one[]" value="E">E</label>
<label><input type="checkbox" name="checkbox-one[]" value="F">F</label>
</p>
<p>
<label>2. QUESTION? </label><br>
<label><input type="checkbox" name="checkbox-two[]" value="А">А</label>
<label><input type="checkbox" name="checkbox-two[]" value="B">B</label>
<label><input type="checkbox" name="checkbox-two[]" value="C">C</label>
<label><input type="checkbox" name="checkbox-two[]" value="D">D</label>
<label><input type="checkbox" name="checkbox-two[]" value="E">E</label>
<label><input type="checkbox" name="checkbox-two[]" value="F">F</label>
</p>
<p>
<label>3. QUESTION? </label><br>
<label><input type="checkbox" name="checkbox-three[]" value="А">А</label>
<label><input type="checkbox" name="checkbox-three[]" value="B">B</label>
<label><input type="checkbox" name="checkbox-three[]" value="C">C</label>
<label><input type="checkbox" name="checkbox-three[]" value="D">D</label>
<label><input type="checkbox" name="checkbox-three[]" value="E">E</label>
<label><input type="checkbox" name="checkbox-three[]" value="F">F</label>
</p>
...
我想是这样的
$one = $_POST['checkbox-one'];
$two = $_POST['checkbox-two'];
$three = $_POST['checkbox-three'];
$four = $_POST['checkbox-four'];
$five = $_POST['checkbox-five'];
$six = $_POST['checkbox-six'];
$seven = $_POST['checkbox-seven'];
$eight = $_POST['checkbox-eight'];
$nine = $_POST['checkbox-nine'];
$ten = $_POST['checkbox-ten'];
$a = $one[0] + $two[0] + $three[0] + $four[0] + $five[0] + $six[0] + $seven[0] + $eight[0] + $nine[0] + $ten[0];
$b = $one[1] + $two[1] + $three[1] + $four[1] + $five[1] + $six[1] + $seven[1] + $eight[1] + $nine[1] + $ten[1];
$c = $one[2] + $two[2] + $three[2] + $four[2] + $five[2] + $six[2] + $seven[2] + $eight[2] + $nine[2] + $ten[2];
$d = $one[3] + $two[3] + $three[3] + $four[3] + $five[3] + $six[3] + $seven[3] + $eight[3] + $nine[3] + $ten[3];
$e = $one[4] + $two[4] + $three[4] + $four[4] + $five[4] + $six[4] + $seven[4] + $eight[4] + $nine[4] + $ten[4];
$f = $one[5] + $two[5] + $three[5] + $four[5] + $five[5] + $six[5] + $seven[5] + $eight[5] + $nine[5] + $ten[5];
答案 0 :(得分:1)
迭代提交的数据并翻转数据键。然后遍历翻转的数据,增加或初始化结果数组中每个的计数。
请注意,您希望在生产表单上进行一些输入验证,但这是您要求的逻辑解决方案的要点。
<form method="POST">
<p>
<label>1. QUESTION? </label><br>
<label><input type="checkbox" name="checkbox-one[]" value="А">А</label>
<label><input type="checkbox" name="checkbox-one[]" value="B">B</label>
<label><input type="checkbox" name="checkbox-one[]" value="C">C</label>
<label><input type="checkbox" name="checkbox-one[]" value="D">D</label>
<label><input type="checkbox" name="checkbox-one[]" value="E">E</label>
<label><input type="checkbox" name="checkbox-one[]" value="F">F</label>
</p>
<p>
<label>2. QUESTION? </label><br>
<label><input type="checkbox" name="checkbox-two[]" value="А">А</label>
<label><input type="checkbox" name="checkbox-two[]" value="B">B</label>
<label><input type="checkbox" name="checkbox-two[]" value="C">C</label>
<label><input type="checkbox" name="checkbox-two[]" value="D">D</label>
<label><input type="checkbox" name="checkbox-two[]" value="E">E</label>
<label><input type="checkbox" name="checkbox-two[]" value="F">F</label>
</p>
<p>
<label>3. QUESTION? </label><br>
<label><input type="checkbox" name="checkbox-three[]" value="А">А</label>
<label><input type="checkbox" name="checkbox-three[]" value="B">B</label>
<label><input type="checkbox" name="checkbox-three[]" value="C">C</label>
<label><input type="checkbox" name="checkbox-three[]" value="D">D</label>
<label><input type="checkbox" name="checkbox-three[]" value="E">E</label>
<label><input type="checkbox" name="checkbox-three[]" value="F">F</label>
</p>
<input type="submit" value="Submit"></input>
</form>
<?php
$result = [];
foreach($_POST as $key => $value){
$value = array_flip($value);
foreach($value as $k => $v){
if(empty($result[$k])){
$result[$k] = 1;
} else {
++$result[$k];
}
}
}
print_r($result);
提交这些复选框时:
你最终会得到这个阵列打印结果:
数组([B] =&gt; 3 [D] =&gt; 1 [E] =&gt; 1 [C] =&gt; 1)