我正在做一个简单的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新手,所以我需要你的帮助。非常感谢那些愿意帮助的人。
答案 0 :(得分:3)
试试这个:
<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>
答案 1 :(得分:2)
使用此
<?php
function shuffle_assoc($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key) {
$random[$key] = $list[$key];
}
return $random;
}
$firstquarter = array('January', 'February', 'March');
$suff = shuffle_assoc($firstquarter);
foreach($suff as $suffle)
{
?>
<label><input type="radio" value="<?php echo $suffle; ?>" name="<?php echo $question; ?>"> <?php echo $suffle; ?></label>
<?php
}
?>
编辑小提琴:http://www.phpfiddle.org/lite/code/rgc-zfj
结果小提琴:http://www.phpfiddle.org/api/run/rgc-zfj
在您的代码中
<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']);
$suff = shuffle_assoc($ans_array);
foreach($suff as $suffle)
{
?>
<h4> <?php echo $number . ".) " . $question; ?></h4>
<label><input type="radio" value="<?php echo $suffle; ?>" name="<?php echo $question; ?>"> <?php echo $suffle; ?></label>
<?php
}
}
?>
<br />
<br />
<input type="submit" value="Submit" name="submit">
</body>
答案 2 :(得分:1)
将所有答案存储在一个数组中,然后随机迭代数组并打印您的答案标签,因为您无法优化查询以随机选择列。