umarrana Posted January 12, 2010 Share Posted January 12, 2010 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 More sharing options...
umarrana Posted January 12, 2010 Author Share Posted January 12, 2010 i am trying this way but its not working for me if($_POST['sbt'] == "Submit Anwsers") { $nofq = 4; for($i = 1; $i <= $nofq; $i++) { if($_POST['q'.$i] == "true") { $msg .= "this is true"; }else { $msg .= "this is false"; } } } Link to comment https://forums.phpfreaks.com/topic/188186-php-quiz-system/#findComment-993502 Share on other sites More sharing options...
ignace Posted January 12, 2010 Share Posted January 12, 2010 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 More sharing options...
umarrana Posted January 12, 2010 Author Share Posted January 12, 2010 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 More sharing options...
ignace Posted January 12, 2010 Share Posted January 12, 2010 Post your code Link to comment https://forums.phpfreaks.com/topic/188186-php-quiz-system/#findComment-993667 Share on other sites More sharing options...
umarrana Posted January 12, 2010 Author Share Posted January 12, 2010 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 More sharing options...
ignace Posted January 12, 2010 Share Posted January 12, 2010 Modify this line $this->setAnswers($possibleAnswers); $this->setAnswers($possibleAnswers ? $possibleAnswers : new ArrayObject()); Please note that the provided code is just an example and was not intended for production usage. Link to comment https://forums.phpfreaks.com/topic/188186-php-quiz-system/#findComment-993671 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.