Jump to content

Recommended Posts

hi dear friends i am working on a simple quiz system can someone help in this here is my form how can i check the true questions in php please help me thanks

 

<form  method="post" name="test">

                <div class="tst">What is Your Name</div> 
                <input type="radio" name="a1" value="false" /> A
                <input type="radio" name="a1" value="true" /> B
                <input type="radio" name="a1" value="false" /> C

                
                <div class="tst">What is Your Country Name</div>
              <input type="radio" name="a2" value="true" /> A
              <input type="radio" name="a2" value="false" /> B
              <input type="radio" name="a2" value="false" /> C
		 <div class="tst">What is Your City name</div>
             <input type="radio" name="a3" value="true" /> A
             <input type="radio" name="a3" value="false" /> B
             <input type="radio" name="a3" value="false" /> C

                
                <div class="tst">What is Your Aera Name</div>
               <input type="radio" name="a4" value="false" /> A
               <input type="radio" name="a4" value="false" /> B
               <input type="radio" name="a4" value="true" /> C
               
               
                <input name="sbt" type="submit" id="sbt" value="Submit Anwsers" />
</form>

Link to comment
https://forums.phpfreaks.com/topic/188186-php-quiz-system/
Share on other sites

I advice you not to add the answer in the source code (true, false) Anyone with a little understanding of HTML can easily see which are the correct answers.

 

<form action="" method="post" id="quiz">
  <div class="question">What is your name?</div>
  <div class="answer">
    <label><input type="radio" name="a[0]" value="A" /> A</label>
    ..
  </div>
  
  <div class="question">What is your country name?</div>
  <div class="answer">
     <label><input type="radio" name="a[1]" value="1" /> 1</label>
     ..
  </div>
  
   ..
</form>

<?php

if ('post' === strtolower($_SERVER['REQUEST_METHOD'])) {
  require_once('class.quiz.php');
  require_once('class.question.php');
  require_once('class.answer.php');
  
  $question = new Question('What is your name?');
  $question->addAnswer(new Answer('A'));
  $question->addAnswer(new Answer('B'));
  ..
  
  $question->setAnswer(new Answer($_POST['a'][0]));
  $question->setCorrectAnswer(new Answer('B'));
  
  $quiz->addQuestion($question);
  echo $quiz->getCorrectAnswersTotal(), '/', $quiz->getQuestionsTotal();
}?>

 

I build my quiz after the user submits his answers however normally you would build it from a SQL query and use it to render your form. Below are the classes used:

 

class Quiz {
  protected $_questions;
  public function __construct(ArrayObject $questions = null) {
    $this->setQuestions($questions ? $questions : new ArrayObject());
  }
  public function setQuestions(ArrayObject $questions) {
    $this->_questions = $questions;
  }
  public function addQuestion(Question $question) {
    $this->getQuestions()->append($question);
  }
  public function addQuestions(ArrayObject $questions) {
    foreach ($questions as $question) {
      $this->addQuestion($question);
    }
  }
  public function getQuestions() {
    return $this->_questions;
  }
  public function getQuestionsTotal() {
    return $this->_questions->count();
  }
  public function getCorrectAnswersTotal() {
    $correctAnswers = 0;
    foreach ($this->getQuestions() as $question) {
      if ($question->answeredCorrect()) ++$correctAnswers;
    }
    return $correctAnswers;
  }
}
class Question {
  protected $_questionText;
  protected $_answers;
  protected $_correctAnswer;
  protected $_answer;
  public function __construct($questionText, ArrayObject $possibleAnswers = null,
      Answer $correctAnswer = null) {
    $this->_questionText = $questionText;
    $this->setAnswers($possibleAnswers);
  }
  public function setCorrectAnswer(Answer $correct) {
    $this->_correctAnswer = $correct;
  }
  public function setAnswer(Answer $answer) {
    $this->_answer = $answer;
  }
  public function setAnswers(ArrayObject $answers) {
    $this->_answers = $answers;
  }
  public function addAnswer(Answer $answer) {
    $this->getAnswers()->append($answer);
  }
  public function addAnswers(ArrayObject $answers) {
    foreach ($answers as $answer) {
      $this->addAnswer($answer);
    }
  }
  public function getAnswers() {
    return $this->_answers;
  }
  public function answeredCorrect() {
    return $this->_correctAnswer->equals($this->_answer);
  }
}
class Answer {
  protected $_answerText;
  public function __construct($answerText) {
    $this->_answerText = $answerText;
  }
  public function getAnswerText() {
    return $this->_answerText;
  }
  public function equals(Answer $answer) {
    return $this->getAnswerText() === $answer->getAnswerText();
  }
}

 

You may also want to add a serialize magic method to store them in the database.

Link to comment
https://forums.phpfreaks.com/topic/188186-php-quiz-system/#findComment-993516
Share on other sites

you are right anyone can see anwser i am must be a stupid

i am trying to use this code but it showing me this error

 

 

 

Catchable fatal error: Argument 1 passed to Question::setAnswers() must be an instance of ArrayObject, null given, called in E:\Websites\www\test\class.question.php on line 9

Link to comment
https://forums.phpfreaks.com/topic/188186-php-quiz-system/#findComment-993599
Share on other sites

class codes are same you posted

<form action="" method="post" id="quiz">
    <div class="question">What is your name?</div>
    <div class="answer">
        <label>
            <input type="radio" name="a[0]" value="A" />
            A</label>
        .. </div>
    <div class="question">What is your country name?</div>
    <div class="answer">
        <label>
            <input type="radio" name="a[1]" value="1" />
            1</label>
        .. </div>
    ..
</form>
<?php 
if ('post' === strtolower($_SERVER['REQUEST_METHOD'])) {
    require_once('class.quiz.php');
    require_once('class.question.php');
    require_once('class.answer.php');
    $question = new Question('What is your name?');
    $question->addAnswer(new Answer('A'));
    $question->addAnswer(new Answer('B'));
    $question->setAnswer(new Answer($_POST['a'][0]));
    $question->setCorrectAnswer(new Answer('B'));
    $quiz->addQuestion($question);
    echo $quiz->getCorrectAnswersTotal(), '/', $quiz->getQuestionsTotal();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/188186-php-quiz-system/#findComment-993668
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.