troubleshooting Question

[php] Issue with Switch Statement

Avatar of prileyosborne
prileyosborne asked on
PHP
6 Comments1 Solution346 ViewsLast Modified:
I have a switch statement I am trying to use in terms of loading individual questions for a test taking website.  I have posted about it before here and I am almost done with everything, but I have one last piece that is throwing me for a loop.

Below is a script that is taking the GET data from the previous question and if there was no previous question, assigning the question data variables a value:

if ($_REQUEST['test_question'] = '' || $_REQUEST['test_question'] = NULL || $_REQUEST['test_question'] = 0) {
    $test_question = 1;
    $_SESSION['current_q'] = 1;

    echo $test_question;

    $get_question = "SELECT test_id, question_primary 
                    FROM create_test_questions 
                    WHERE test_id = '$test_id'
                    ORDER BY question_primary 
                    LIMIT 1";
    $result = mysql_query($get_question);
    $row = mysql_fetch_row($result);

    // Set variable for question id
    $test_question_id   = $row['question_primary'];
    $_SESSION['question_primary'] = $test_question_id;

  } else {
    $test_question      = $_SESSION['current_q']+1;
    $test_question_id   = $_SESSION['question_primary']+1;
    $get_question = "SELECT question_primary 
                    FROM create_test_questions
                    WHERE question_primary = '$test_question_id'
                    LIMIT 1";
    $result = mysql_query($get_question);
    $row = mysql_fetch_row($result);

  };

Then below is a switch statement that is based on the value of the variable $test_question
switch ($_REQUEST['test_question']){
    case ($_REQUEST['test_question'] = 1):
      
      echo "It is the first question";
      //echo $test_id;
      //echo $test_question;
      
      $get_question = "SELECT test_id, step_number, step_number_sub, step_required, step_description, step_outcome, question_primary 
                      FROM create_test_questions 
                      WHERE test_id = '$test_id'
                      ORDER BY question_primary 
                      LIMIT 1";
      $result = mysql_query($get_question);
      $row = mysql_fetch_row($result);

      // Assign variables to populate question
      $test_id            = $row['0'];
      $step_number        = $row['1'];
      $step_number_sub    = $row['2'];
      $step_required      = $row['3'];
      $step_description   = $row['4'];
      $step_outcome       = $row['5'];
      $test_question_id   = $row['6'];


      // Prepare variables for grabbing next question
      $test_question = $_REQUEST['test_question'] + 1;
      $_SESSION['current_q'] = $test_question;

      $test_question_id = $test_question_id + 1;
      $_SESSION['question_primary'] = $test_question_id;
      break;

    case ($_REQUEST['test_question'] > 1 && $_REQUEST['test_question'] < $q):
      
      echo "It is a middle question";
      echo $test_question;
      // Get the next question
      $get_question = "SELECT test_id, step_number, step_number_sub, step_required, step_description, step_outcome, question_primary 
                      FROM create_test_questions
                      WHERE question_primary = $test_question_id
                      LIMIT 1";
      $result = mysql_query($get_question);
      $row = mysql_fetch_row($result);

      // Assign variables to populate question
      $test_id              = $row['test_id'];
      $step_number          = $row['step_number'];
      $step_number_sub      = $row['step_number_sub'];
      $step_required        = $row['step_required'];
      $step_description     = $row['step_description'];
      $step_outcome         = $row['step_outcome'];
      $test_question_id     = $row['question_primary'];


      // Prepare variables for grabbing next question
      $test_question = $_REQUEST['test_question'] + 1;
      $_SESSION['current_q'] = $test_question;

      $test_question_id = $test_question_id + 1;
      $_SESSION['question_primary'] = $test_question_id;

      // Load previous test data to the database
      if ($_REQUEST['step_number'] = NULL) {
        $load_result = "INSERT INTO `create_test_results` test_id, res_step_number, res_step_sub_number, res_step_outcome, res_step_notes, rest_test_taker) 
                      VALUES ('$_SESSION[test_id]','$_REQUEST[step_number]','$_REQUEST[step_sub_number]','$_REQUEST[step_outcome]','$_REQUEST[step_notes]','$_REQUEST[test_taker]')";
        $load = mysql_query($load_result);
      }
      break;

    case ($_REQUEST['test_question'] = $q):
      
      echo "It is a last question";

      // Load previous test data to the database
      $load_result = "INSERT INTO `create_test_results` test_id, res_step_number, res_step_sub_number, res_step_outcome, res_step_notes, rest_test_taker) 
      VALUES ('$_SESSION[test_id]','$_REQUEST[step_number]','$_REQUEST[step_sub_number]','$_REQUEST[step_outcome]','$_REQUEST[step_notes]','$_REQUEST[test_taker]')";
      $load = mysql_query($load_result);

      // Hide the form and then complete the case
      ?>

      <script type="text/javascript">$('#form').hide()</script>

      <?php
      echo "<h3>You have completed the test and your results have been submitted. Thank you for your time.</h3>";
      echo "<p>If you would like to take another test, please click here to view the list of available tests</p>";
      $_SESSION['question_primary'] = '';
      break;

    case ($test_question > $q || $test_question < 1):

      echo "It is the error";
      $_SESSION['current_q']      = 1;
      $_SESSION['question_primary']   = 1;
      echo "Error submitting test. Conteact Webmaster";
      break;
    
    default:
      
      echo "It is the default";
      $get_question = "SELECT test_id, step_number, step_number_sub, step_required, step_description, step_outcome, question_primary 
      FROM create_test_questions 
      WHERE question_primary = '$test_id'
      ORDER BY question_primary 
      LIMIT 1";
      $result = mysql_query($get_question);
      $row = mysql_fetch_row($result);

      // Assign variables to populate question
      $test_id            = $row['test_id'];
      $step_number        = $row['step_number'];
      $step_number_sub    = $row['step_number_sub'];
      $step_required      = $row['step_required'];
      $step_description   = $row['step_description'];
      $step_outcome       = $row['step_outcome'];
      $test_question_id   = $row['question_primary'];


      // Prepare variables for grabbing next question
      $test_question = $_REQUEST['test_question'] + 1;
      $_SESSION['current_q'] = $test_question;

      $test_question_id = $test_question_id + 1;
      $_SESSION['question_primary'] = $test_question_id;
      break;
  };

I am able to get the first question to show, but it never goes to the next question when I click the next button.

Thanks for any help you can offer and please let me know if you need any other information.  Thanks again!
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros