[英]Randomising 2 PHP Arrays - working now
現在可以使用。
我需要我的頁面使用這2個數組將問題隨機化,但是“問題”和“答案”需要匹配,我不知道該怎么做。
<?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']; ?>
<?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>
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--;
}
制作鍵數組並將其隨機化。 在下面的行中更改for($i = 0; $i<$numofquestions; $i++)
$keys = array_keys($question);
shuffle($keys);
foreach($keys as $i)
這意味着您需要匹配正確的答案對嗎? 如果是這樣的話:
您可以將問題和答案與多維數組相關聯,並在使用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")
);
這只是一個想法,希望對您有所幫助
我嘗試了一下您的代碼,進行了一些調整,將$ 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;
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.