PHP中的测验 - 我这样做了吗?

时间:2011-11-07 04:37:13

标签: php

我正在尝试在php中进行简单的三个问题测验。用户登录后,他们开始测验,并有15分钟完成它(我有会议部分计算出来)。一个真/假,一个多项选择和一个简短答案。每个问题在每个页面上单独显示

我的问题是到目前为止我的逻辑是否正确。由于多个语法错误,此程序将无法运行。它告诉我删除它们应该在那里的括号。例如,在第55行,它告诉我应该删除最后一个}

  if ($selected_radio == 'false') {
  correct()
  }

有人可以看看这个,并给我一些关于去哪里的指示?如果我完全偏离正轨,请告诉我如何向前迈进。例子很棒。

我感谢你的时间。由于这很长,我在这里上传了代码:http://paste.bradleygill.com/index.php?paste_id=339313

我的代码:

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

if (!session_is_registered("number"))
{
$_SESSION["number"] = 0;
$_SESSION["answer"] = 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"];
$answer = $_SESSION["answer"];
$correct = $_SESSION["correct"];


if ($number == 0){
$answer = "false";
$_SESSION["answer"] = $answer;
 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()
}
else {
   wrong();
}
}
}


if ($number == 1){
$answer = "Pennsylvania";
$_SESSION["answer"] = $answer;
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'])) {

$selected_checkbox = $_POST['question2'];

if ($selected_checkbox = = 'Pennslyvania') {
   correct($correct)
}
else {
   wrong($answer);
}
}


if ($number == 2){
$answer = "lee";
$_SESSION["answer"] = $answer;
print <<<THIRD
<p>5. 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($correct)
}
else {
   wrong($answer);
WRONG;
}
}
}



function correct($correct) {
$correct++;
$_SESSION["correct"] = $correct;
return $correct;
print <<<CORRECT
  You are correct!  Good Job!
  <br /><br />
CORRECT;
 }

function wrong($answer) {
print <<<WRONG
  Sorry, the correct answer is: $answer.
  <br /><br />
WRONG;

}


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>
$question
<input type = "text" name = "answer" value = "" size = "5" />
<input type = "submit" value = "Check Answer" />
</form>
 FORM;
 }

print <<<BOTTOM
</body>
</html>
BOTTOM;
?>

1 个答案:

答案 0 :(得分:1)

不,这不是正确的做法。老实说,这里有太多的内容甚至是真正的批评。发布较小的样本会更容易。

我突然想到了一些事情:

  1. 重复代码 - 希望将其纳入函数
  2. 逻辑,数据和HTML都混合在一起。这不好。
  3. 这是人们在嘲笑PHP时所指的代码。对不起,如果这听起来很刺耳,但这是真的。