SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Creating a quiz

  1. #1
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Creating a quiz

    Hi,
    I am working a quiz containing 6 questions and every 6 questions have 5 answers to choose from and input type is radio button, my problem is that a user can check any combination of answers and if i do a manual conditional statement and creating them, it would be like 35 something if statements.which is a nuts situation. is there another way?
    All those who wander aren't lost.

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    If I understand you correctly you have 6 questions with 5 possible answers to the questions - I assume only one answer will be correct so that you only need one if statement for each question. If the answer is correct else the answer was wrong.

    To avoid if() you could use switch which may be a bit shorter.

  3. #3
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Imagine nearly 70 possible loop or switch statements based on what person answer that statement get pulled, i want to avoid making loops 70 times, this is my code, it got a 3-4 loops, in each loop you can see the terms changes as it pulls answer from different table (terms), i want a smarter way.instead of repeating loop 70 times.
    All those who wander aren't lost.

  4. #4
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Might I suggest you make one query and build a multidimensional array containing each category and series of questions. You then check if a key exists and if not show questions. If there is a key you check if key exists at the next level and so on. The POST results could be saved to session at each stage to be used at final outcome of the quiz.

  5. #5
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    but how, i can make pseudo code but not logical, as i m not good in php.
    All those who wander aren't lost.

  6. #6
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Here's a rough example
    PHP Code:
    <?php
    session_start
    ();

    $questions = array(
        
    "question1" => array(
            
    "answer1a",
            
    "answer1b",
            
    "answer1c",
            
    "answer1d",
            
    "answer1e"),
        
    "question2" => array(
            
    "answer2a",
            
    "answer2b",
            
    "answer2c",
            
    "answer2d",
            
    "answer2e"),
        
    "question3" => array(
            
    "answer3a",
            
    "answer3b",
            
    "answer3c",
            
    "answer3d",
            
    "answer3e"),
        
    "question4" => array(
            
    "answer4a",
            
    "answer4b",
            
    "answer4c",
            
    "answer4d",
            
    "answer4e"),
        
    "question5" => array(
            
    "answer5a",
            
    "answer5b",
            
    "answer5c",
            
    "answer5d",
            
    "answer5e"),
        
    "question6" => array(
            
    "answer6a",
            
    "answer6b",
            
    "answer6c",
            
    "answer6d",
            
    "answer6e")
    );
                                  
    ///////////////////////////////////////
    /////Set answer session if not set/////
    ///////////////////////////////////////
    if (!isset($_SESSION['answers'])){$_SESSION['answers'] = array();}

    ///////////////////////////////////////
    ///////////// Processing //////////////
    /////////////////////////////////////// 
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        foreach(
    $_POST as $question => $a){
            
    //Only use question keys//
            
    if($question != "submit" && $question != "showresult"){
                
    $_SESSION['answers'][$question] = $a;
            }
        }
    }

    ///Build answer display or could be used to insert to DB

    if(isset($_POST['showresult'])){
        
    $display "";
        foreach(
    $_SESSION['answers'] as $question => $a){
            
    $display .= "$question":" "$a <br />\r";
        }
    }

    ?>
    <html>
    <body>
    <?php
    echo '<form action="" method="post">';
        
    $q_num count($_SESSION['answers']) + 1;    
        
    $currentQ "question" $q_num;
          
        
    //check to see if question is available//
        
    if(array_key_exists($currentQ,$questions)){
        
            
    //check that session answers is set and question key is not in array
            
    if(isset($_SESSION['answers']) && !array_key_exists($currentQ,$_SESSION['answers'])){
                foreach(
    $questions[$currentQ] as $k => $qtext){
                    echo 
    "<input type=\"radio\" name=\"$currentQ\" value=\"$k\" />$qtext<br />\r";
                }
            }
            echo 
    '<input type="submit" name="submit" value="Submit" />';
        }else{
            echo 
    '<input type="submit" name="showresult" value="Show Result" />';    
        }
    echo 
    '</form>';

    if(isset(
    $display)){ echo $display;}
    ?>
    </body>
    </html>

  7. #7
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Now my task is to understand it.
    All those who wander aren't lost.

  8. #8
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Here's a slightly different version that allows more flexibility for questions but will require two arrays with matching numeric keys. Although you can't see these keys in the questions array they are there (0,1,2 etc) just like the answer array.
    PHP Code:
    <?php
    session_start
    ();

    $questions = array(    
        
    "This is question one",
        
    "This is question two",
        
    "This is question three",
        
    "This is question four",
        
    "This is question five",
        
    "This is question six"
    );
    $answers = array(
        
        
    => array(
            
    "answer1a",
            
    "answer1b",
            
    "answer1c",
            
    "answer1d",
            
    "answer1e"),
        
    => array(
            
    "answer2a",
            
    "answer2b",
            
    "answer2c",
            
    "answer2d",
            
    "answer2e"),
        
    => array(
            
    "answer3a",
            
    "answer3b",
            
    "answer3c",
            
    "answer3d",
            
    "answer3e"),
        
    => array(
            
    "answer4a",
            
    "answer4b",
            
    "answer4c",
            
    "answer4d",
            
    "answer4e"),
        
    => array(
            
    "answer5a",
            
    "answer5b",
            
    "answer5c",
            
    "answer5d",
            
    "answer5e"),
        
    => array(
            
    "answer6a",
            
    "answer6b",
            
    "answer6c",
            
    "answer6d",
            
    "answer6e")
    );    
                                 
    ///////////////////////////////////////
    /////// Clear session answers ///////// 
    ///////////////////////////////////////
    if(isset($_POST['clearresults'])){
        unset(
    $_SESSION['answers']);
    }
                                  
    ///////////////////////////////////////
    /////Set answer session if not set/////
    ///////////////////////////////////////
    if (!isset($_SESSION['answers'])){$_SESSION['answers'] = array();}

    ///////////////////////////////////////
    ///////////// Processing //////////////
    /////////////////////////////////////// 
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        foreach(
    $_POST as $question => $a){
            
    //Only use question keys//
            
    if($question != "submit" && $question != "showresult" && $question != "clearresults"){
                
    $_SESSION['answers'][$question] = $a;
            }
        }
    }

    ///Build answer display or could be used to insert to DB

    if(isset($_POST['showresult'])){
        
    $display "";
        foreach(
    $_SESSION['answers'] as $question => $a){
            
    $question str_replace("_"" ",$question);
            
    $display .= "$question":" "$a <br />\r";
        }
    }

    ?>
    <html>
    <body>
    <?php
    echo '<form action="" method="post">';
        
    $currentQ count($_SESSION['answers']);
          
        
    //check to see if question is available//
        
    if(array_key_exists($currentQ,$questions)){
        
            
    //check that session answers is set and question key is not in array
            
    if(isset($_SESSION['answers']) && !array_key_exists($currentQ,$_SESSION['answers'])){
                foreach(
    $answers[$currentQ] as $k => $qtext){
                    echo 
    "<input type=\"radio\" name=\"{$questions[$currentQ]}\" value=\"$k\" />$qtext<br />\r";
                }
            }
            echo 
    '<input type="submit" name="submit" value="Submit" />';
        }else{
            
    $button = (empty($display) ? '<input type="submit" name="showresult" value="Show Result" />' '<input type="submit" name="clearresults" value="Clear Results" />');
            echo 
    $button;
        }
    echo 
    '</form>';

    if(isset(
    $display)){ echo $display;}
    /*
    echo "<pre>";
    print_r($questions);      
    echo "</pre>";
    echo "<pre>";
    print_r($answers);      
    echo "</pre>"; 
    echo "<pre>";
    print_r($_SESSION['answers']);      
    echo "</pre>"; 
    */
    ?>
    Now if the question being asked is not sequential, but rather based on the last answer, then the arrays and whole process will need to be different.

  9. #9
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Ha ha, This isn't Jeopardy, listing a bunch of answers without the question. Here's a version with the question.

    PHP Code:
    <?php
    session_start
    ();

    $questions = array(    
        
    "This is question one",
        
    "This is question two",
        
    "This is question three",
        
    "This is question four",
        
    "This is question five",
        
    "This is question six"
    );
    $answers = array(
        
        
    => array(
            
    "answer1a",
            
    "answer1b",
            
    "answer1c",
            
    "answer1d",
            
    "answer1e"),
        
    => array(
            
    "answer2a",
            
    "answer2b",
            
    "answer2c",
            
    "answer2d",
            
    "answer2e"),
        
    => array(
            
    "answer3a",
            
    "answer3b",
            
    "answer3c",
            
    "answer3d",
            
    "answer3e"),
        
    => array(
            
    "answer4a",
            
    "answer4b",
            
    "answer4c",
            
    "answer4d",
            
    "answer4e"),
        
    => array(
            
    "answer5a",
            
    "answer5b",
            
    "answer5c",
            
    "answer5d",
            
    "answer5e"),
        
    => array(
            
    "answer6a",
            
    "answer6b",
            
    "answer6c",
            
    "answer6d",
            
    "answer6e")
    );    
                                 
    ///////////////////////////////////////
    /////// Clear session answers ///////// 
    ///////////////////////////////////////
    if(isset($_POST['clearresults'])){
        unset(
    $_SESSION['answers']);
    }
                                  
    ///////////////////////////////////////
    /////Set answer session if not set/////
    ///////////////////////////////////////
    if (!isset($_SESSION['answers'])){$_SESSION['answers'] = array();}

    ///////////////////////////////////////
    ///////////// Processing //////////////
    /////////////////////////////////////// 
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        foreach(
    $_POST as $question => $a){
            
    //Only use question keys//
            
    if($question != "submit" && $question != "showresult" && $question != "clearresults"){
                
    $_SESSION['answers'][$question] = $a;
            }
        }
    }

    ///Build answer display or could be used to insert to DB

    if(isset($_POST['showresult'])){
        
    $display "";
        foreach(
    $_SESSION['answers'] as $question => $a){
            
    $question str_replace("_"" ",$question);
            
    $display .= "$question":" "$a <br />\r";
        }
    }

    ?>
    <html>
    <body>
    <?php
    echo '<form action="" method="post">';
        
    $currentQ count($_SESSION['answers']);
          
        
    //check to see if question is available//
        
    if(array_key_exists($currentQ,$questions)){
        
            
    //check that session answers is set and question key is not in array
            
    if(isset($_SESSION['answers']) && !array_key_exists($currentQ,$_SESSION['answers'])){
                echo 
    "<b>{$questions[$currentQ]}</b><br />\r";
                foreach(
    $answers[$currentQ] as $k => $qtext){
                    echo 
    "<input type=\"radio\" name=\"{$questions[$currentQ]}\" value=\"$k\" />$qtext<br />\r";
                }
            }
            echo 
    '<input type="submit" name="submit" value="Submit" />';
        }else{
            
    $button = (empty($display) ? '<input type="submit" name="showresult" value="Show Result" />' '<input type="submit" name="clearresults" value="Clear Results" />');
            echo 
    $button;
        }
    echo 
    '</form>';

    if(isset(
    $display)){ echo $display;}
    /*
    echo "<pre>";
    print_r($questions);      
    echo "</pre>";
    echo "<pre>";
    print_r($answers);      
    echo "</pre>"; 
    echo "<pre>";
    print_r($_SESSION['answers']);      
    echo "</pre>"; 
    */
    ?>
    </body>
    </html>


Bookmarks

Posting Permissions

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