title: One quiz item per page (php/mysql quiz program)
body: <p>I'm currently working on a quiz program using PHP/mySQL (my first time ever actually using either).</p>
  
  <p>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.</p>
  
  <p>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";".</p>
  
  <p>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.</p>
  
  <p>Here's my quiz.php page (start page for any quiz):</p>
  
  <pre><code>&lt;?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;
  ?&gt;
  
  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;Quiz &lt;?php echo mysql_real_escape_string($_GET['id']);?&gt;&lt;/title&gt;
      &lt;link href="css/bootstrap.min.css" rel="stylesheet"&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div id="container" style="margin-left: 30px;"&gt;
  
  
  &lt;!-- create the header using the quiz id and name --&gt;
  &lt;h1 style="text-align: center;"&gt;Quiz &lt;?php echo $quiz['id'] ?&gt;: &lt;?php echo $quiz['name'] ?&gt;&lt;/h1&gt;
  
  &lt;hr /&gt;
  
  &lt;h4&gt;This quiz will have a total of &lt;?php echo $length?&gt; questions.&lt;/h4&gt;
  
  &lt;button onClick="parent.location='start.php'"&gt;Begin Quiz&lt;/button&gt;
  
      &lt;/div&gt;
      &lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>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?</p>
  
  <pre><code>&lt;?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'];
  ?&gt;
  
  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;Quiz &lt;?php echo mysql_real_escape_string($_GET['id']);?&gt;&lt;/title&gt;
      &lt;link href="css/bootstrap.min.css" rel="stylesheet"&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div id="container" style="margin-left: 30px;"&gt;
  
  &lt;h3&gt;Question &lt;?php echo $counter ?&gt; of &lt;?php echo $length ?&gt;&lt;/h3&gt;
  
  &lt;hr /&gt;
  
  &lt;h4&gt;&lt;?php echo $current_question['prompt']?&gt;&lt;/h4&gt;
  
  &lt;?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");
      }
      ?&gt;
  
          &lt;p style="margin-left: 50px;"&gt;
          &lt;?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
              ?&gt;
              &lt;input type='radio' name='question_&lt;?php echo $current_question['id']; ?&gt;' value='&lt;?php echo $a_row['prompt']?&gt;'&gt;&lt;?php echo $a_row['prompt']?&gt;
              &lt;br /&gt;
  
          &lt;?php
          }
          echo "&lt;/p&gt;";
      }
  
      // if question type is "short answer", create text box
      elseif ($current_question['if_short_answer'] == 1) {
          ?&gt;
          &lt;input type="text" name="question_&lt;?php echo $current_question['id']; ?&gt;" /&gt;&lt;br /&gt;
      &lt;/p&gt;
  
      &lt;?php
      } ?&gt;
  
  
  &lt;?php
  if ($counter &gt;= 1) { ?&gt;
  &lt;button onClick="parent.location='next.php'"&gt;Next Question&lt;/button&gt;
  &lt;?php
  }
  
  else { ?&gt;
      &lt;button onClick="parent.location='start.php'"&gt;Begin Quiz&lt;/button&gt;
  &lt;?php
  }
  ?&gt;
  
      &lt;/div&gt;
      &lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>I basically need guidance in writing my script to progress from one question to the next.</p>
  
  <p>Thanks!</p>