Reputation: 457
need a little help in solving the problem with the counting of the responses checkboxes. There is a form of 10 questions each have 6 checkboxes with the ability to select multiple options. Possible answers A, B, C, D, E and F. It is necessary to take all the responses and summarized in the form of 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>
...
I think like this
$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];
Upvotes: 1
Views: 467
Reputation: 40861
Iterate through your submitted data and flip the keys of the data. Then iterate through the flipped data incrementing or initializing the count of each in a results array.
Note you'll want to do some input validation on a production form, but this is the jist for a solution for the logic about which you were asking.
<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);
When submitting these checkboxes:
You'll end up with this array printed results:
Array ( [B] => 3 [D] => 1 [E] => 1 [C] => 1 )
Upvotes: 1