gpt4 book ai didi

php - 比较通过while循环动态生成的单选按钮的选定值并显示分数

转载 作者:行者123 更新时间:2023-11-29 22:30:29 25 4
gpt4 key购买 nike

我正在使用 php 和 mysql 作为数据库创建一个考试页面。我通过 while 循环显示问题和选项。

我面临的问题是从用户那里获取选定的值。

如有任何帮助,我们将不胜感激!

这是代码:

<body>
<form name="form1" method="POST" action="">
<input name="submit" type="submit" value="Start Test">

<?php
$username="root";
$password="";
$database="assesment";
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if(isset($_POST['submit']))
{
$query = "SELECT * FROM demo";
$result = mysql_query($query);
$num = mysql_num_rows($result);
mysql_close();
echo "<b><center>Database Output</center></b><br><br>";
$i=0;while ($i < $num)
{
$qno = mysql_result($result,$i,"qno");
$quest = mysql_result($result,$i,"quest");
$a = mysql_result($result,$i,"a");
$b = mysql_result($result,$i,"b");
$c = mysql_result($result,$i,"c");
$d = mysql_result($result,$i,"d");
echo "
<br><br><b> $qno.&nbsp;&nbsp;$quest<br><br>
Options : </b>
<input type='Radio' Name='Answer $qno' value='A' >A.<span><span> $a &nbsp;&nbsp;
<input type='Radio' Name='Answer $qno' value='B' >B.<span><span> $b &nbsp;&nbsp;
<input type='Radio' Name='Answer $qno' value='C' >C.<span><span> $c &nbsp;&nbsp;
<input type='Radio' Name='Answer $qno' value='D' >D.<span><span> $d &nbsp;&nbsp;
<br><br>";
$i++;
}
}

最佳答案

首先,mysql_*函数已被弃用,不应使用。使用 MySQLi 或 PDO。用 MySQLi 发布我的答案。

我会更改为不同的 while循环:

<body>
<form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php

if(isset($_POST['submit']) && $_POST['submit'] == "Start Test"){
$username="root";
$password="";
$database="assesment";
$sql = mysqli_connect("localhost", $username, $password, $database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM demo";
$result = mysqli_query($sql, $query);
$num = mysqli_num_rows($result);

echo "<b><center>Database Output</center></b><br><br>";

while ($row = mysqli_fetch_assoc($result)) {
$qno = $row['qno'];
$quest = $row['quest'];
$a = $row['a'];
$b = $row['b'];
$c = $row['c'];
$d = $row['d'];
echo "<br><br><b> $qno.&nbsp;&nbsp;$quest<br><br>\r\n"
echo "Options : </b>\r\n";
echo "<input type='Radio' Name='Answer[$qno]' value='A' >A.<span><span> $a &nbsp;&nbsp;\r\n";
echo "<input type='Radio' Name='Answer[$qno]' value='B' >B.<span><span> $b &nbsp;&nbsp;\r\n";
echo "<input type='Radio' Name='Answer[$qno]' value='C' >C.<span><span> $c &nbsp;&nbsp;\r\n";
echo "<input type='Radio' Name='Answer[$qno]' value='D' >D.<span><span> $d &nbsp;&nbsp;\r\n"
echo "<br><br>";
}

mysqli_free_result($result);
echo "<input name='submit' type='submit' value='See Results'>\r\n";
mysqli_close($sql);

} elseif(isset($_POST['submit']) && $_POST['submit'] == "See Results") {
// Review answers and show the correct ones
foreach($_POST['Answer'] as $answer){
// Iterate over answers...
}
} else {
echo "<input name='submit' type='submit' value='Start Test'>\r\n";
}
?>
</form>
</body>

您可能还需要考虑将“提交”按钮移至末尾,但只要所有内容都包含在 <form> 内即可。标签,它应该提交整个表单。

关于答案,这取决于您存储它们的位置。但是,如果将它们放入相似的数组中,则可以循环它并比较结果。例如:

} elseif(isset($_POST['submit']) && $_POST['submit'] == "See Results") {
// Review answers and show the correct ones
$userAnswers = $_POST['Answer'];
// Connect to DB and get Answer Key code... put in array called $keyAnswers
$resultAnswers = array();
for($i=0;$i<count($keyAnswers);$i++){
if(isset($userAnswers[$i])){
// Compare if user answered the question
$resultAnswers[$i] = ($userAnswers[$i] == $keyAnswers[$i])?TRUE:FALSE;
} else {
// Answer is wrong if they did not answer it
$resultAnswers[$i] = FALSE;
}
}
foreach($resultAnswers as $k => $v){
if($v){
echo "You answered Question $k correctly.<br />\r\n";
} else {
echo "You answered Question $k incorrectly.<br />\r\n";
}
}
}

关于php - 比较通过while循环动态生成的单选按钮的选定值并显示分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29845245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com