Jump to content

dave378

New Members
  • Posts

    1
  • Joined

  • Last visited

dave378's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I'm working on a project that users can enter their name to complete a multiple choice quiz, while administrators can log in to add new questions and edit and delete existing questions. I've created a multiple choice quiz in PHP and MySQL that loops questions on the same page, instead of multiple pages. Basically, you click on the submit button to go to the next set of questions, but on the same page. The questions are data retrieved from a database I created in PhpMyAdmin. The problem now is I have no idea what code to refer to when trying to calculate answers in the final results page, so I was wondering if someone could help me out. Underneath is the code I have for my quiz.php page, and now I want to calculate how many questions I have answered correctly. I'd really appreciate the help. <?php //CODE FOR QUIZ.PHP //Code retrieves the name that the user typed in the text-field. session_start(); require 'includes/connection.php'; $name = $_REQUEST['name']; $_SESSION['name'] = $name; //User's name is displayed above the quiz. echo 'Welcome, '.$name; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>World War II Quiz</title> </head> <body> <?php //Start with question ID if (isset($_GET['id'])) { //get question id from user and store $question_id = $_GET['id']; } else { $question_id = 1; //no question id submitted //unset session data unset($_SESSION['questions']); unset($_SESSION['total']); } //If the questions array has been built if (isset($_SESSION['questions'])) { //has the user selected a radio button if (isset($_REQUEST['value'])) { //check for correct if ($_SESSION['answer'] == $_REQUEST['value']) { //correct answer $_SESSION['total']++; } } else { //No value inputted from the user echo "hey bozo click a button"; $question_id--; } } //Questions array is not been built else { //get a list of question id's $sql = "SELECT question_id FROM questions ORDER BY question_id ASC"; $results = mysql_query($sql,$conn); $stringtemp = ""; $maxtemp = 1; while ($row = mysql_fetch_assoc($results)) //More results { $stringtemp = $stringtemp." ".$row['question_id']; $maxtemp++; } //Create question id array pulled from database $_SESSION['questions'] = explode(" ",$stringtemp); //Max value of the array $_SESSION['max']=$maxtemp; } $sql = "select * from questions where question_id =".$_SESSION['questions'][$question_id]; $results = mysql_query($sql,$conn); $row = mysql_fetch_array($results); //Store correct answer for next pageload $_SESSION['answer'] = $row['correct']; $nextid=$question_id+1; if ($nextid == $_SESSION['max']) { //Final question $next = "results.php"; } else { $_SESSION['name'] = $name; //Not the final question $next = "quiz.php?id=".$nextid; } echo "<form action=\"".$next."\" method=\"post\" name=\"form\" > <p>".$row['question_text']."</p> <input type=\"radio\" name=\"value\" value=\"1\">".$row['answer1']."<br> <input type=\"radio\" name=\"value\" value=\"2\">".$row['answer2']."<br> <input type=\"radio\" name=\"value\" value=\"3\">".$row['answer3']."<br> <input type=\"radio\" name=\"value\" value=\"4\">".$row['answer4']."<br> <input class=\"submit\" type=\"submit\" value=\"Submit\"/> </form> </body> </html> "; ?>
  2. Hi everyone, My name is Dave and I hope to be posting here regularly. My knowledge in PHP ranges from basic to intermediate, and I'm currently learning MySQL too. I'm still learning quite a bit in PHP and I hope the more I'll learn, the more I can help fellow newcomers too. Thanks.
×
×
  • 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.