Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我正在制作一个PHP测试应用程序。如何确保问题只被问一次?_Php_Arrays_Forms_Session - Fatal编程技术网

我正在制作一个PHP测试应用程序。如何确保问题只被问一次?

我正在制作一个PHP测试应用程序。如何确保问题只被问一次?,php,arrays,forms,session,Php,Arrays,Forms,Session,我有十个问题和答案。我使用arry_rand和shuffle将问题随机排列,并将答案随机排列 如何确保问题只被问一次?在我把所有十个问题循环一遍之后,我希望游戏结束。谢谢你的帮助!:) 这是我到目前为止所拥有的 include(“inc/questions.php”); 会话_start(); $pageTitle=“数学测验:加法”; $_会话[“分数”]=0; 如果((isset($_会话[“分数”])和&$choices[0][“正确答案”])){ $\会话[“分数”]+=1; } ech

我有十个问题和答案。我使用
arry_rand
shuffle
将问题随机排列,并将答案随机排列

如何确保问题只被问一次?在我把所有十个问题循环一遍之后,我希望游戏结束。谢谢你的帮助!:)

这是我到目前为止所拥有的

include(“inc/questions.php”);
会话_start();
$pageTitle=“数学测验:加法”;
$_会话[“分数”]=0;
如果((isset($_会话[“分数”])和&$choices[0][“正确答案”])){
$\会话[“分数”]+=1;
}
echo$_会话[“分数”];
$rand=array_rand($problems,1);
洗牌(问题);
$choices=[
$questions[0][“correctAnswer”],
$questions[0][“firstIncorrectAnswer”],
$questions[0][“secondIncorrectAnswer”],
];
洗牌($选择);
如果(!isset($_会话[“计数器”])|$_会话[“计数器”]>9)){
$\会话[“计数器”]=1;
}否则{
$\会话[“计数器”]+=1;
}
?>

而不是:

$rand = array_rand($questions,1);
shuffle($questions);



您可以通过在每个会话中只对问题进行一次洗牌来实现这一点,并将洗牌后的版本保留在会话变量中,每次弹出一个问题

其他一些问题:

  • 您每次都将分数重置为零
  • 要检查用户提交的答案是否正确,您需要查看
    $\u POST[]
    数组
您可以使用以下代码:

// Check submitted answer is correct:
if (isset($_SESSION["correctAnswer"]) && isset($_POST[$_SESSION["correctAnswer"]])) {
    $_SESSION["score"] += 1;
}

if (!isset($_SESSION["score"]) {
    $_SESSION["score"] = 0;
    $_SESSION["counter"] = 0;
    shuffle($questions);
    $_SESSION["questions"] = $questions;
}

echo "score = " . $_SESSION["score"];

if (count($_SESSION["questions"]) == 0) {
    // Here you should navigate to a game-over page that 
    // displays the score and allows to start again (clearing the session).
    die ("No more questions");
}

// Get next question
$currQuestion = array_pop($_SESSION["questions"]);
$_SESSION["correctAnswer"] = $currQuestion["correctAnswer"];

$choices = [
    $currQuestion["correctAnswer"],
    $currQuestion["firstIncorrectAnswer"],
    $currQuestion["secondIncorrectAnswer"],
];
shuffle($choices);

$_SESSION["counter"] += 1;

在代码的其余部分,将出现的
$questions[0]
替换为
$currQuestion

非常感谢!!我现在正在工作,但一到家就会试试!在我工作的时候,我会更深入地研究$\u POST数组!谢谢你的帮助。我已经挣扎了好几天了。我现在在工作,但我回家后会试试的。
// Check submitted answer is correct:
if (isset($_SESSION["correctAnswer"]) && isset($_POST[$_SESSION["correctAnswer"]])) {
    $_SESSION["score"] += 1;
}

if (!isset($_SESSION["score"]) {
    $_SESSION["score"] = 0;
    $_SESSION["counter"] = 0;
    shuffle($questions);
    $_SESSION["questions"] = $questions;
}

echo "score = " . $_SESSION["score"];

if (count($_SESSION["questions"]) == 0) {
    // Here you should navigate to a game-over page that 
    // displays the score and allows to start again (clearing the session).
    die ("No more questions");
}

// Get next question
$currQuestion = array_pop($_SESSION["questions"]);
$_SESSION["correctAnswer"] = $currQuestion["correctAnswer"];

$choices = [
    $currQuestion["correctAnswer"],
    $currQuestion["firstIncorrectAnswer"],
    $currQuestion["secondIncorrectAnswer"],
];
shuffle($choices);

$_SESSION["counter"] += 1;