PHP 在单选按钮上显示随机记录 [测验] [英] Php display random records on radio button [quiz]

查看:31
本文介绍了PHP 在单选按钮上显示随机记录 [测验]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm doing a simple php quiz game. I have the questions and answers stored in the database. The table structure of my tblQuiz is like this:

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

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

I have to randomly display them on the page.

这是我迄今为止尝试过的:

This is what I've tried so far:

<!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>

但我似乎无法得到我想要达到的目标,只是随机显示了问题.我也想自己随机定位答案.

But I can't seem to get what I want to achieve, only the questions are being randomly displayed. I also want to randomly position the answers themselves.

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

Im newbie in PHP so I need your help. Thanks a lot in advance to those who will help.

推荐答案

试试这个:

<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 在单选按钮上显示随机记录 [测验]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆