变量不会在PHP脚本中递增

时间:2011-11-07 22:15:39

标签: php

首先:这不是this question的重复。相同的脚本,但不同的问题。

我最初认为我之前的问题中的错误会解决此错误,但事实并非如此。

我正在通过PHP进行简单的3个问题测验。测验运行没有语法错误,但问题是我的变量$ correct似乎没有在整个测验中递增(给出正确的答案)。我把print语句看看是否有变化,但没有打印出来。不知何故,它没有正确地增加变量的值。

而不是输出:

 You got 2 out of 3 correct. //where 2 is the value of $correct

输出:

You got correct out of correct. //where the value $correct seems to have no value

我做错了什么?

 <?php
 ini_set('session.gc_maxlifetime',900);
//echo ini_get("session.gc_maxlifetime"); 
session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}

 if(isset($_SESSION["blah"]))
{
$_SESSION["number"] = 0;
$_SESSION["correct"] = 0;
}

$total_number = 3;

print <<<TOP
<html>
<head>
<title> History Quiz </title>
</head>
<body>
<h3> History Quiz </h3>
TOP;

$number = $_SESSION["number"];
$correct = $_SESSION["correct"];


if ($number == 0){
print <<<FIRST
<p> You will be given $total_number questions in this quiz. <br /><br/>
  You will have 15 minutes to complete it. <br /><br/>
  You cannot go back to change previous answers.<br /><br/>
  Here is your first question: <br /><br />
</p>
<p>1. Abe Lincoln was born in Illinois.</p>
<p>
    <label><input type="radio" name="question1" value="true" /> True </label>
    <label><input type="radio" name="question1" value="false" /> False </label>
</p>
FIRST;

if (isset($_POST['submit'])) {

$selected_radio = $_POST['question1'];

if ($selected_radio == 'false') {
   $correct++;
print $correct;
   $_SESSION["correct"] = $correct;

}
}
}


if ($number == 1){
print <<<SECOND
<p>2. In what state was the battle of Gettysburg fought?</p>
<p>
    <label><input type="checkbox" name="question2" value="Texas" /> a) Texas  
</label><br/>
    <label><input type="checkbox" name="question2" value="Pennsylvania" /> b)  
Pennsylvania </label><br/>
    <label><input type="checkbox" name="question2" value="Virginia" /> c) Virginia   
</label><br/>
    <label><input type="checkbox" name="question2" value="West Virginia" /> d) West   
Virginia </label>
</p>
SECOND;

if (isset($_POST['submit'])) {

if(isset($_POST['question2']) &&
$_POST['question'] == 'Pennsylvania')
{
   $correct++;
   $_SESSION["correct"] = $correct;  
}

}
}


if ($number == 2){
print <<<THIRD
<p>3. The last name of the commander of the Army of North Virginia was __________.</p>
<p>
    <input type='text' id='question3' />
THIRD;

if (isset($_POST['submit'])) {

$selected_answer = $_POST['question3'];

if ($selected_answer == "lee") {
   $correct++;
   $_SESSION["correct"] = $correct;
 }
 }
 }


if ($number >= $total_number)
{
print <<<FINAL_SCORE
Your final score is $correct correct out of $total_number. <br /><br />
Thank you for playing. <br /><br />
FINAL_SCORE;
session_destroy();
}
else
{
$number++;
$_SESSION["number"] = $number;
$script = $_SERVER['PHP_SELF'];
print <<<FORM
<form method = "post" action = $script>
<input type = "submit" value = "Check Answer" />
</form>
FORM;
}

?>

Julio,我目前的代码(第二次更新):

<?php
ini_set('session.gc_maxlifetime',900);
session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}

if(isset($_SESSION["correct"])){
$correct = $_SESSION["correct"];
} else {
$number = 0;
$correct = 0;
}

// check if which question was submitted
if (isset($_POST['submit'])) {
// set $number = the question that was submitted
$number = $_POST['question'];
switch ($_POST['question']){
    case 1:
        if ($_POST['answer']) $correct++; // this answer should be 'true'
        break;
    case 2:
        if ($_POST['answer'] == 2) $correct++; // this answer should be 'PA'
        break;
    case 3:
    if ($_POST['answer'] == "lee") $correct++; //this answer should be 'lee'
}
}   

// set the session correct var to our current tally
$_SESSION['correct'] = $correct;



$total_number = 3;

print <<<TOP
<html>
<head>
<title> History Quiz </title>
</head>
<body>
<h3> History Quiz </h3>
TOP;

if ($number == 0){
print <<<FIRST
<p> You will be given $total_number questions in this quiz. <br /><br/>
  You will have 15 minutes to complete it. <br /><br/>
  You cannot go back to change previous answers.<br /><br/>
  Here is your first question: <br /><br />
</p>
<p>1. Abe Lincoln was born in Illinois.</p>
<p>
    <label><input type="radio" name="answer" value="true" /> True </label>
    <label><input type="radio" name="answer" value="false" /> False </label>
    <input type="hidden" name="question" value="1" />
</p>
FIRST;
}


if ($number == 1){
print <<<SECOND
<p>2. In what state was the battle of Gettysburg fought?</p>
<p>
<label><input type="checkbox" name="answer" value="1" /> a) Texas  
</label><br/>
<label><input type="checkbox" name="answer" value="2" /> b)  
Pennsylvania </label><br/>
<label><input type="checkbox" name="answer" value="3" /> c) Virginia   
</label><br/>
<label><input type="checkbox" name="answer" value="4" /> d) West   
Virginia </label>
<input type="hidden" name="question" value="2" />
</p>
SECOND;
}
if ($number == 2){
print <<<THIRD
 <p>3. The last name of the commander of the Army of North Virginia was __________.</p>
 <p>
    <input type='text' id='answer' />
<input type="hidden" name="question" value="3" />
 </p>
THIRD;
}

if ($number >= $total_number)
{
print <<<FINAL_SCORE
Your final score is $correct correct out of $total_number. <br /><br />
Thank you for playing. <br /><br />
FINAL_SCORE;
session_destroy();
}
else {
 $number++;
 $_SESSION['number'] = $number;
 $script = $_SERVER['PHP_SELF'];
 print <<<FORM
<form method = "post" action = $script>
<input type = "submit" value = "Check Answer" />
</form>
FORM;
}


?>

3 个答案:

答案 0 :(得分:0)

你的前几行......

if(isset($_SESSION["blah"]))
{
  $_SESSION["number"] = 0;
  $_SESSION["correct"] = 0;
}

这意味着如果将$ _SESSION ['blah']变量设置为开头,$ _SESSION ['correct']将只有一个值。这可能是您的问题,因为如果$ _SESSION ['correct']没有值,则$ session中的任何增量和赋值都不会进一步降低。

答案 1 :(得分:0)

从快速阅读代码看起来,您通过发布自己来刷新页面 - 这意味着您每次都将$correct变量重置为0。如果要增加$correct,请增加SESSION中的值,然后将会话$correct值读出到页面的$correct var中,以便在每个表单提交时不重置它。 / p>

编辑添加一些示例代码:

<?php

session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}

if(isset($_SESSION["correct"])){
    $correct    = $_SESSION["correct"];
} else {
    $number     = 0;
    $correct    = 0;
}

// check if which question was submitted
if (isset($_POST['submit'])) {
    // set $number = the question that was submitted
    $number = $_POST['question'];
    switch ($_POST['question']){ //<-----replace this line
        case 1:
            if ($_POST['answer']) $correct++; // this answer should be 'true'
            break;
        case 2:
            if ($_POST['answer'] == 2) $correct++; // this answer should be 'PA'
            break;
        //etc.
    }
}   

// set the session correct var to our current tally
$_SESSION['correct'] = $correct;



$total_number = 3;

print <<<TOP
<html>
<head>
<title> History Quiz </title>
</head>
<body>
<h3> History Quiz </h3>
TOP;

if ($number == 0){
    print <<<FIRST
    <p> You will be given $total_number questions in this quiz. <br /><br/>
      You will have 15 minutes to complete it. <br /><br/>
      You cannot go back to change previous answers.<br /><br/>
      Here is your first question: <br /><br />
    </p>
    <p>1. Abe Lincoln was born in Illinois.</p>
    <p>
        <label><input type="radio" name="answer" value="true" /> True </label>
        <label><input type="radio" name="answer" value="false" /> False </label>
        <input type="hidden" name="question" value="1" />
    </p>
    FIRST;
}


if ($number == 1){
print <<<SECOND
<p>2. In what state was the battle of Gettysburg fought?</p>
<p>
    <label><input type="checkbox" name="answer" value="1" /> a) Texas  
</label><br/>
    <label><input type="checkbox" name="answer" value="2" /> b)  
Pennsylvania </label><br/>
    <label><input type="checkbox" name="answer" value="3" /> c) Virginia   
</label><br/>
    <label><input type="checkbox" name="answer" value="4" /> d) West   
Virginia </label>
<input type="hidden" name="question" value="2" />
</p>
SECOND;
etc. . .

答案 2 :(得分:0)

我会拿出

if(isset($_SESSION["blah"]))
{
  $_SESSION["number"] = 0;
  $_SESSION["correct"] = 0;
}
完全

这意味着它会在页面刷新时重置。