为了账号安全,请及时绑定邮箱和手机立即绑定

如何计算答案 a,b,c,d,e,f 被给出的次数?

如何计算答案 a,b,c,d,e,f 被给出的次数?

PHP
杨__羊羊 2022-01-08 20:24:29
我想计算我选择一个选项的次数。那么我选择了多少次 A,B,C,D,E,F 。我尝试进行某种形式的计数,但没有奏效。<form action="Antwoord.php" method="POST">    <input type="text" name="Naam" placeholder="Uw naam:"><?php   $sql = "SELECT * FROM  questionlist_choice WHERE Qid='1'";   $result = mysqli_query($conn, $sql);  if ($result->num_rows > 0) {    $i = 0;  while ($row = mysqli_fetch_assoc($result)) {$vraag = $row['Vraag'];$vraagA = $row['Vraag_keuzeA'];$vraagB = $row['Vraag_keuzeB'];$vraagC = $row['Vraag_keuzeC'];$vraagD = $row['Vraag_keuzeD'];$vraagE = $row['Vraag_keuzeE'];$vraagF = $row['Vraag_keuzeF'];$kikio =  "<div>     <br><p>$vraag</p>     <input type='radio' name='q[$i]' value='A'required> $vraagA<br>     <input type='radio' name='q[$i]' value='B'required> $vraagB<br>     <input type='radio' name='q[$i]' value='C'required> $vraagC<br>     <input type='radio' name='q[$i]' value='D'required> $vraagD<br>     <input type='radio' name='q[$i]' value='E'required> $vraagE<br>     <input type='radio' name='q[$i]' value='F'required> $vraagF<br> </div>"; echo $kikio;$i++;  }}?><input type="submit"></form>我期待一个回声或其他东西,显示我选择了多少次 A 被选择了 B 等等。
查看完整描述

1 回答

?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

您可以使用array_count_values()来了解提供了每个答案的数量:


$counts = array_count_values($_POST['q']);

// e.g.: ["A" => 2, "B" => 1, "F" => 1]

然后,您可以通过查看结果来回显每个答案的数字:


echo "A was answered " . ($counts["A"] ?? 0) . " time(s)<br>";

echo "B was answered " . ($counts["B"] ?? 0) . " time(s)<br>";

echo "C was answered " . ($counts["C"] ?? 0) . " time(s)<br>";

echo "D was answered " . ($counts["D"] ?? 0) . " time(s)<br>";

echo "E was answered " . ($counts["E"] ?? 0) . " time(s)<br>";

echo "F was answered " . ($counts["F"] ?? 0) . " time(s)<br>";

如果您只想列出实际给出的答案,则可以遍历$counts数组:


foreach ($counts as $answer => $count) {

    echo $answer . " was answered " . $count . " time(s)<br>";

}


查看完整回答
反对 回复 2022-01-08
  • 1 回答
  • 0 关注
  • 138 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信