Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to calculate results for a complicated multiple choice quiz?

    I've been working on a multiple choice quiz in PHP recently.

    I've noticed that a lot of tutorials online tend to focus on simple one page quizzes. What I'm trying to do is to create a multiple-choice that contain more than one page. The quiz lasts for twelve questions, and each page contains three questions.

    At the moment, I'm trying calculate from 0 to 9 to figure out how many questions I've answered correctly, but I can't calculate any answers. It doesn't matter if I answer the question correctly, in the end I'm always getting a "0/9" as a final score.

    I've uploaded "txt" containing my PHP code, including my final results page if anyone is interested. I'd appreciate any help!
    Attached Files Attached Files
    Last edited by davo398; 08-30-2013 at 03:35 PM.

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,004
    Thanks
    15
    Thanked 236 Times in 236 Posts
    Suggestion, make a folder, put an index.php page in the folder, as the questions are answered, just include different stuff in the page. If you include a file as a .txt it will simply be written verbatim to the page. Use if($_SESSION['whatever']=="somevalue') include "./thisfile.txt"; or php, to set conditions for the includes.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Solved it!

    I forgot to say thanks to DrDOS for giving me the idea to work it out; I feel like a jerk for not mentioning for so long.

    I thought I'd share what I created. What I did for each quiz page was that I applied the number 1 in the value for the right multiple choice answer (i.e. <input type="radio" value="1"/>). For all the other incorrect answers, I typed in 0 for the values (i.e. <input type="radio" value="0"/>).

    In the final results.php page I called the names of the questions, and then I incremented totalAnswersCorrect to add all the correct answers together. In the end, I divided all the total correct answers out of 12.

    Underneath is my working results.php file.

    PHP Code:

    <?php
    /*FINAL WORKING [B]RESULTS.PHP[/B] PAGE*/
    $_SESSION['Question1']=$_REQUEST['Question1'];
    $_SESSION['Question2']=$_REQUEST['Question2'];
    $_SESSION['Question3']=$_REQUEST['Question3'];
    $_SESSION['Question4']=$_REQUEST['Question4'];
    $_SESSION['Question5']=$_REQUEST['Question5'];
    $_SESSION['Question6']=$_REQUEST['Question6'];
    $_SESSION['Question7']=$_REQUEST['Question7'];
    $_SESSION['Question8']=$_REQUEST['Question8'];
    $_SESSION['Question9']=$_REQUEST['Question9'];
    $_SESSION['Question10']=$_REQUEST['Question10'];
    $_SESSION['Question11']=$_REQUEST['Question11'];
    $_SESSION['Question12']=$_REQUEST['Question12'];
    ?>
    <!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>Movie Buff Quiz</title>
    </head>
    <body>

    <?php
    //Calculates the correct answers altogether.
    $totalAnswersCorrect=$_SESSION['Question1']+$_SESSION['Question2']+$_SESSION['Question3']+$_SESSION['Question4']+$_SESSION['Question5']+$_SESSION['Question6']+$_SESSION['Question7']+$_SESSION['Question8']+$_SESSION['Question9']+$_SESSION['Question10']+$_SESSION['Question11']+$_SESSION['Question12'];
    //Determines how many answers are
    echo " ".$totalAnswersCorrect." / 12  ";
    " ";
     if (
    $totalAnswersCorrect>=10){
        echo 
    "Congratulations!";
    }

    else if(
    $totalAnswersCorrect<=5){
        echo 
    "Not good enough. Try again.";
        
    } else if (
    $totalAnswersCorrect>=6){
        echo 
    "Okay $name, but give it another go.";
        }

    ?>

    </body>
    </html>
    Now I need help on another multiple choice quiz, although this time it's a bit more complicated. What I've been trying to do is to create another quiz 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. Unlike the first project I was working on, the questions are being retrieved from the database. And unlike the other example, 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. If so, please feel free to look at the attachment containing my quiz.php text file, thanks.
    Attached Files Attached Files
    Last edited by davo398; 10-19-2013 at 02:09 PM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •