Link to home
Start Free TrialLog in
Avatar of prileyosborne
prileyosborne

asked on

[php] Issue with Switch Statement

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);

  };

Open in new window


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;
  };

Open in new window


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!
Avatar of StingRaY
StingRaY
Flag of Thailand image

The switch...case conditions need constant not expression. What you are trying to do is the latter. Convert them into if...else could help.
ASKER CERTIFIED SOLUTION
Avatar of StingRaY
StingRaY
Flag of Thailand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Line 68 has a } that should not be there.
Nope, I am wrong. Sorry.
Avatar of prileyosborne
prileyosborne

ASKER

Thank you so much! That was super helpful!
Line 68, in my reply, should be double equal sign "==" not "=", like this:

} elseif ($_REQUEST['test_question'] == $q) {

Open in new window


Sorry for the mistake.