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

我想计算我选择一个选项的次数。那么我选择了多少次 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 等等。


杨__羊羊
浏览 139回答 1
1回答

小唯快跑啊

您可以使用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) {&nbsp; &nbsp; echo $answer . " was answered " . $count . " time(s)<br>";}
打开App,查看更多内容
随时随地看视频慕课网APP