gpt4 book ai didi

Php在单选按钮上显示随机记录[测验]

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

我正在做一个简单的 php 问答游戏。我将问题和答案存储在数据库中。我的 tblQuiz 的表结构是这样的:

_id, question, answer1, answer2, answer3, correctanswer.

我必须在页面上随机显示它们。

这是我到目前为止尝试过的:

<!DOCTYPE html>
<html>
<head>
<title>Sample Quiz</title>
<?php
include 'db.php';
$stmt = $conn->prepare( "SELECT *
FROM tblquiz ORDER BY rand()" );
$stmt->execute();
?>
</head>
<body>
<?php
$number = 0;
for($i=0; $row = $stmt->fetch(); $i++){
$number++;
$id = $row['_id'];
$question = $row['question'];
$answer1 = $row['answer1'];
$answer2 = $row['answer2'];
$answer3 = $row['answer3'];
$correctanswer = $row['correctanswer'];
?>

<h4> <?php echo $number . ".) " . $question; ?></h4>
<label><input type="radio" value="<?php echo $answer1; ?>" name="<?php echo $question; ?>"> <?php echo $answer1; ?></label>
<label><input type="radio" value="<?php echo $answer2; ?>" name="<?php echo $question; ?>"> <?php echo $answer2; ?></label>
<label><input type="radio" value="<?php echo $answer3; ?>" name="<?php echo $question; ?>"> <?php echo $answer3; ?></label>
<label><input type="radio" value="<?php echo $correctanswer; ?>" name="<?php echo $question; ?>"> <?php echo $correctanswer; ?></label>

<?php
}
?>
<br />
<br />
<input type="submit" value="Submit" name="submit">

</body>
</html>

但我似乎得不到我想要达到的效果,只有问题在随机显示。我还想自己随机放置答案。

我是 PHP 新手,所以我需要您的帮助。非常感谢那些愿意提供帮助的人。

最佳答案

试试这个:

<body>
<?php
$number = 0;
for($i=0; $row = $stmt->fetch(); $i++){
$number++;
$id = $row['_id'];
$question = $row['question'];
$ans_array = array($row['answer1'],$row['answer2'],$row['answer3'],$row['correctanswer']);
shuffle($ans_array);
?>

<h4> <?php echo $number . ".) " . $question; ?></h4>
<label><input type="radio" value="<?php echo $ans_array[0]; ?>" name="<?php echo $question; ?>"> <?php echo $ans_array[0]; ?></label>
<label><input type="radio" value="<?php echo $ans_array[1]; ?>" name="<?php echo $question; ?>"> <?php echo $ans_array[1]; ?></label>
<label><input type="radio" value="<?php echo $ans_array[2]; ?>" name="<?php echo $question; ?>"> <?php echo $ans_array[2]; ?></label>
<label><input type="radio" value="<?php echo $ans_array[3]; ?>" name="<?php echo $question; ?>"> <?php echo $ans_array[3]; ?></label>

<?php
}
?>
<br />
<br />
<input type="submit" value="Submit" name="submit">

</body>

关于Php在单选按钮上显示随机记录[测验],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21178084/

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