Jakemmarsh
Jakemmarsh

Reputation: 4671

One quiz item per page (php/mysql quiz program)

I'm currently working on a quiz program using PHP/mySQL (my first time ever actually using either).

Up until now I was just working on getting it all functioning correctly with the mySQL tables, and to do so I was putting all questions in a quiz on one page. However, now I want to be able to put just one question per page and then progress by submitting one answer at a time.

The way my questions are chosen to be in the quiz may be a little confusing. They all have a "quiz_id" column that corresponds to the quiz they're in. The quiz has a "length" column that specifies how many questions it will actually have. So there can be more questions with the corresponding "quiz_id" than will actually be in the quiz. The way I randomly select which questions will be included is with "$question = "SELECT * FROM questions WHERE quiz = '$id' ORDER BY rand() LIMIT $length";".

Now I'm having a lot of trouble putting just one question per page. This is because each time you progress, the next random question needs to be chosen, as long as we haven't reached the limit of $length number of questions. A counter also needs to increase that keeps track of what number question you are on, out of how many ($length). I'm not sure if I need to have two separate action scripts.. one to begin the quiz, and then one to progress between questions.

Here's my quiz.php page (start page for any quiz):

<?php
// initialize the mysql data
$connect = mysql_connect('localhost', 'root', '');
$select_db = mysql_select_db('mysql');

// define the id and length from url
$id = mysql_real_escape_string($_GET['id']);
$length = mysql_real_escape_string($_GET['length']);

// query quiz table for all columns
$query_quiz = "SELECT * FROM quizzes WHERE id = '$id' LIMIT 1";

// if quiz query fails
if(!$query_quiz_result = mysql_query($query_quiz)) 
    { 
        die("Couldn't run quiz query"); 
    } 

// fetch whole array of quiz info
$quiz = mysql_fetch_array($query_quiz_result);



//query question table for all columns
$question = "SELECT * FROM questions WHERE quiz = '$id' ORDER BY  rand() LIMIT $length";
$q_result = mysql_query($question) or die ("couldn't run questions query");


// store queried questions as an array to pass via session variables
$q_array = array();
while($row = mysql_fetch_assoc($q_result))
{
    $q_array[] = $row;
}


session_start();
$_SESSION['quiz'] = $id;
$_SESSION['questions'] = $q_array;
$_SESSION['length'] = $length;
?>

<html>
  <head>
    <title>Quiz <?php echo mysql_real_escape_string($_GET['id']);?></title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
    <div id="container" style="margin-left: 30px;">


<!-- create the header using the quiz id and name -->
<h1 style="text-align: center;">Quiz <?php echo $quiz['id'] ?>: <?php echo $quiz['name'] ?></h1>

<hr />

<h4>This quiz will have a total of <?php echo $length?> questions.</h4>

<button onClick="parent.location='start.php'">Begin Quiz</button>

    </div>
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

And here's my start.php page.. not sure if I can use this one page for all the questions, or do I need a separate action page for once the quiz has begun and you're progressing past #1?

<?php
// continue the session
session_start();

// initialize the mysql data
$connect = mysql_connect('localhost', 'root', '');
$select_db = mysql_select_db('mysql');


$quiz_id = $_SESSION['quiz'];
$length = $_SESSION['length'];
$_SESSION['questions_array'] = $_SESSION['questions'];
$current_question = array_shift($_SESSION['questions_array']);
$_SESSION['counter'] = 1;

$counter = $_SESSION['counter'];
?>

<html>
  <head>
    <title>Quiz <?php echo mysql_real_escape_string($_GET['id']);?></title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
    <div id="container" style="margin-left: 30px;">

<h3>Question <?php echo $counter ?> of <?php echo $length ?></h3>

<hr />

<h4><?php echo $current_question['prompt']?></h4>

<?php
// define query for answers for each question
    $answers = 'SELECT `prompt` FROM `answers` WHERE `quiz` = '.$quiz_id.' AND `question` = '.$current_question['id'].'';

    //if failed
    if(!$answers_result = mysql_query($answers)){
        die("Couldn't run answers query");
    }
    ?>

        <p style="margin-left: 50px;">
        <?php

    // if question type is "multiple choice", loop through answer choices and print them as options
    if ($current_question['if_short_answer'] == 0) {
        // loop through rows of answer choices
        while($a_row = mysql_fetch_array($answers_result))
        {
            // print each answer choice
            ?>
            <input type='radio' name='question_<?php echo $current_question['id']; ?>' value='<?php echo $a_row['prompt']?>'><?php echo $a_row['prompt']?>
            <br />

        <?php
        }
        echo "</p>";
    }

    // if question type is "short answer", create text box
    elseif ($current_question['if_short_answer'] == 1) {
        ?>
        <input type="text" name="question_<?php echo $current_question['id']; ?>" /><br />
    </p>

    <?php
    } ?>


<?php
if ($counter >= 1) { ?>
<button onClick="parent.location='next.php'">Next Question</button>
<?php
}

else { ?>
    <button onClick="parent.location='start.php'">Begin Quiz</button>
<?php
}
?>

    </div>
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

I apologize if this is a very vague or long question.. I've just gotten myself pretty lost now and not sure how to proceed. Basically I am asking: how can I put just one question per page, progressing through random questions until a limit of $length is reached, and then a "submit quiz" button at the end leads to a score page? All along the way, the "next question" button needs to store the user's input for the current question.

I basically need guidance in writing my script to progress from one question to the next.

Thanks!

Upvotes: 0

Views: 15116

Answers (2)

muni
muni

Reputation: 21

All you need to get the all questions from Database, and using jquery show()/hide() method show only one questions at a time.

I had written sample script for your requirement here.

http://www.smarttutorials.net/responsive-quiz-application-using-php-mysql-jquery-ajax-and-twitter-bootstrap/

Upvotes: 2

Naveen Kumar
Naveen Kumar

Reputation: 4601

Firstly you are re creating the $q_array on each page so it will only contain the last displayed question which will not work

try

if(isset($_SESSION['questions']))
{
   $_SESSION['questions']+= ", $question_id";       
}

In the select query you should omit the questions with the already displayed in the array.

Upvotes: 0

Related Questions