立即行动。
我需要使用这两个数组来随机化问题,但问题是'并且回答'需要匹配,我不知道该怎么做。
<?php
$question=[
"Favourite game?","Favourite Drink?","Favourite Fast Food?","Favourite TV show?","Favourite chocolate?"
];
$answer=[
["Super Mario","Battlefield", "Counter Strike", "Fifa"],
["Coke","Tea","coffee","Water"],
["Pizza","Fish and Chips","Burger","Hot Dog"],
["The Walking Dead","Prison Break","Breaking Bad","Game of Thrones"],
["Mars","Snickers","Diary Milk","Galaxy"]];
?>
<form action="results.php" method="get">
<input type="hidden" name="timer" id="timer"/>
<p>You have 120 seconds (2 minutes) to answer <?php print $numofquestions ?> question(s)</p>
<br>
<?php
for($i = 0; $i<$numofquestions; $i++)
{
print "<p>".$question[$i]."</p><select name='answer[]'>";
for($j = 0; $j<count($answer[$i]); $j++)
{
print "<option value = '".($j + 1)."'>".$answer[$i][$j]." </option>";
}
print "</select>";
}
?>
<br>
<br>
<input type="submit" name="submit" value="Submit Answers" onclick="sendto()">
</form>
上面的解决方案已经解决,但现在又出现了第二个问题。当使用这个PHP代码从上一页打印选定数量的问题时,它会打印1个额外的问题.----立即工作!
此代码允许我在questions.php
中获取问题的数量
?php
session_start();
$_SESSION['firstname'] = $_GET['firstname'];
$fname = $_SESSION['firstname'];
$_SESSION['numofquestions'] = $_GET['numofquestions'];
$numofquestions = $_SESSION['numofquestions'];
?>
&#13;
中的php
<?php
$question=[
"Favourite game?","Favourite Drink?","Favourite Fast Food?","Favourite TV show?","Favourite chocolate?"
];
$answer=[
["Super Mario","Battlefield", "Counter Strike", "Fifa"],
["Coke","Tea","coffee","Water"],
["Pizza","Fish and Chips","Burger","Hot Dog"],
["The Walking Dead","Prison Break","Breaking Bad","Game of Thrones"],
["Mars","Snickers","Diary Milk","Galaxy"]];
?>
<form action="results.php" method="get">
<input type="hidden" name="timer" id="timer"/>
<p>You have 120 seconds (2 minutes) to answer <?php print $numofquestions ?> question(s)</p>
<br>
<?php
$keys = array_keys($question);
shuffle($keys);
$cnt = $numofquestions;
foreach($keys as $i)
{
print "<p>".$question[$i]."</p><select name='answer[]'>";
for($j = 0; $j<count($answer[$i]); $j++)
{
print "<option value = '".($j + 1)."'>".$answer[$i][$j]."</option>";
}
print "</select>";
if(!$cnt--) break;
}
?>
<br>
<br>
<input type="submit" name="submit" value="Submit Answers" onclick="sendto()">
</form>
答案 0 :(得分:3)
的index.php:
<html>
<head>
<title>Guessing Game</title>
<link rel="stylesheet" type="text/css" href="css/mainstyle.css">
</head>
<body>
<h1> Guessing Game </h1>
<br>
<p>In this guessing game, I will be able to guess your age.</p>
<form action="questions.php" method="post">
<p>Please enter your name:</p>
<input type="text" name="firstname" />
<p>How many questions would you like to answer?[1-5]</p>
<select name="numofquestions" />
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input name="submit" type="submit" value="Start"/>
</form>
</body>
</html>
questions.php
<?php
session_start();
$_SESSION['numofquestions'] = isset($_POST['numofquestions']) ? intval($_POST['numofquestions']) : 5;
$_SESSION['name'] = htmlspecialchars($_POST['firstname']);
$questions = [
"Favourite game?","Favourite Drink?","Favourite Fast Food?","Favourite TV show?","Favourite chocolate?"
];
$_SESSION['questions'] = $questions;
$questions = array_slice($questions, 0, $_SESSION['numofquestions']);
$answers =[
["Super Mario","Battlefield", "Counter Strike", "Fifa"],
["Coke","Tea","coffee","Water"],
["Pizza","Fish and Chips","Burger","Hot Dog"],
["The Walking Dead","Prison Break","Breaking Bad","Game of Thrones"],
["Mars","Snickers","Diary Milk","Galaxy"]
];
?>
<form action="results.php" method="post">
<input type="hidden" name="timer" id="timer"/>
<p>You have 120 seconds (2 minutes) to answer <?php echo count($questions) ?> question(s)</p>
<br>
<?php
$questionKeys = array_keys($questions);
shuffle($questionKeys);
foreach($questionKeys as $questionID){
echo $questions[$questionID];
echo '<select name="answer'.$questionID.'">';
foreach($answers[$questionID] as $answer){
echo '<option value="' . $answer . '">'.$answer.'</option>';
}
echo '</select><br>';
}
?>
<br>
<br>
<input type="submit" name="submit" value="Submit Answers" >
</form>
results.php
<?php
session_start();
echo '<h1>Results for: ' . $_SESSION['name'] . '</h1>';
$counter = intval($_SESSION['numofquestions']) -1;
while($counter >= 0){
echo $_SESSION['questions'][$counter] . ' has value ' . $_POST['answer' . $counter] . '<br>';
$counter--;
}
答案 1 :(得分:0)
制作一系列密钥并将其随机化。使用以下行更改for($i = 0; $i<$numofquestions; $i++)
$keys = array_keys($question);
shuffle($keys);
foreach($keys as $i)
答案 2 :(得分:0)
意味着您需要与正确答案匹配吗?如果是这样的话:
你可以用多维数组和使用array_key_exists之后将问题和答案联系起来,就像那样
<?php
$question = array(1 => "Favourite game?",
2 => "Favourite Drink?",
3 => "Favourite Fast Food?",
4 => "Favourite TV show?",
5 => "Favourite chocolate?");
$answer = array(
1 => array('right_answer' => "Super Mario", "Battlefield", "Counter Strike", "Fifa"),
2 => array("Coke","Tea",'right_answer' => "coffee","Water"),
3 => array("Pizza","Fish and Chips",'right_answer' => "Burger","Hot Dog"),
4 => array("The Walking Dead",'right_answer' => "Prison Break","Breaking Bad","Game of Thrones"),
5 => array("Mars",'right_answer' => "Snickers","Diary Milk","Galaxy")
);
这只是一种意识形态,希望有所帮助
答案 3 :(得分:0)
我尝试了你的代码,调整了一下,我限制了$ keys数组,只包含用户指定的问题数量。至少有两种等效的方法可以实现相同的行为。
<?php
//This is less error prone
$numofquestions = $_GET['numofquestions'];
for($i = 0; $i < $numofquestions; $i++) {
$question_trimmed[$i] = $question[$i];
}
$keys = array_keys($question_trimmed);
shuffle($keys);
和
<?php
//this is shorter
$numofquestions = $_GET['numofquestions'];
$keys = range(0, $numofquestions - 1);
shuffle($keys);
试试它们,并告诉我们您的问题是否已解决
注意:您不需要$cnt
变量或最后一个条件语句if(!$cnt--) break;
。