How to display only one data at a time in an online quiz

好久不见. 提交于 2019-12-24 02:16:28

问题


I've previously made a text file and turned it into multidimensional array to display as the questions for my quiz.

Note: I am unable to insert images therefore I cannot provide any example so I'll try to be as descriptive as I can.

I'm trying to display only one question at a time, every time a user clicks on my quiz.

This is my code so far. The main.php page:

<h2>ONLINE QUIZ</h2>
<ul>
    <li><a href='question.php'>Take quiz</a></li>
    <li><a href='module.php'>Admin Module</a></li>
</ul>
<?php

$file = fopen('data.txt', 'r');

$array = array();
while ($line = fgetcsv($file)) {
    $array[] = $line;
}

fclose($file);

session_start();
$_SESSION["questions_array"]=$array;

?>

And the question.php page:

<?php

session_start();
$array=$_SESSION["questions_array"];

foreach ($array as $q => $data) {
    echo '<p>'.array_shift($data).'</p>';
    foreach ($data as $a => $answer) {
        echo 
             '  <input type="radio" name="question-'.$q.'" id="question-'.$q.'"'.
             '         value="'.$a.'"/>'.
             '  <label for="question-'.$q.'">'.$answer.'</label>'.
             '<br>';
    }

}

?>

When the Take quiz link is clicked, the user is taken to the question page where only one question is shown. The user then picks an answer and hits submit. This submit button will take the user to the result page where they can then hit continue.

The Continue link will redirect them back to the question page where the next question is displayed.

From stuff that I've done before, I am attempting to use the isset() function to make this happen. However, the problem is that I'm not sure how exactly to write my isset().

I've found this snippet from this site, I'm not sure if it's useful, but:

if (!isset($_SESSION['FirstVisit'])) {

    //show site for the first time part
    $_SESSION['FirstVisit] = 1;
    header("Location: http://example.com/index.php");

    // Don't forget to add http colon slash slash www dot before!

    } else { Show normal site }

But once again I found myself blank. How exactly do I use the isset() to display only one question?


回答1:


I kinda get what your asking, Ive jotted out the basic structure. This can all be done on one page, hope it helps.

EDIT:

As im such a nice guy heres a complete script, using my prev suggestion ;p

<?php
session_start();
echo '<h2>ONLINE QUIZ</h2>';


//Scores
if($_SERVER['REQUEST_METHOD']=='GET' && isset($_GET['scores'])){
    echo 'Basic output for scores';
    echo '<pre>';
    print_r($_SESSION['answers']);
    echo '</pre>';
    unset($_SESSION['answers']);
    unset($_SESSION['question']);
}


//Session question/array is set
if(isset($_SESSION['question']) && isset($_SESSION['questions_array'])){

    //Handle prev question post
    if($_SERVER['REQUEST_METHOD']=='POST'){
        //process prev question
        $_SESSION['answers'][$_SESSION['question']-1]=(0+$_POST['answer']);
    }

    if($_SESSION['question'] < $_SESSION['total_question']){
        $q=$_SESSION['question'];
        //EDIT - Shuffle answers for output
        //Hold the question into a var
        $question = $_SESSION['questions_array'][$q][0];
        //unset the question from the array
        unset($_SESSION['questions_array'][$q][0]);
        //put all the pos answers into a new array
        $answers = $_SESSION['questions_array'][$q];
        //shuffle the answers
        shuffle($answers);


        echo '<form method="POST" action="">
              <h3>'.$question.'</h3>';
        //loop through the answers
        foreach($answers as $key=>$value){
            //if the value is nothing cont to next, removed question key 0
            if($value==''){continue;}else{
                echo '<p><input type="radio" value="'.$value.'" name="answer">'.$value.'</p>';
            }
        }
        echo '<p><input type="submit" value="Submit"></p>
        </form>';

    }else{
        //Quiz Complete
        echo 'Test Complete <a href="'.basename($_SERVER["SCRIPT_FILENAME"]).'?scores=1">Check scores</a>';
    }

    //Assign next question to session
    $_SESSION['question']++;
}else{

    //Pages first load so show quiz index
    $_SESSION['question']=0;
    get_questions();
?>
    <ul>
        <li><a href='<?=basename($_SERVER["SCRIPT_FILENAME"]);?>'>Take quiz</a></li>
        <li><a href='module.php'>Admin Module</a></li>
    </ul>
    <?php
}

//Function to put questions in session
function get_questions(){
    $file = fopen('data.txt', 'r');
    $array = array();
    while ($line = fgetcsv($file,1000,',')) {
        $array[] = $line;
    }
    fclose($file);
    $_SESSION['questions_array']=$array;
    $_SESSION['total_question']=count($array);
    return;
}
?>



回答2:


Okay so the solution to your problem to do another array shift on the outter array. By this you can pull off one question and print it out. The quiz is preserved in the session, so the next time the page loads (i.e. the continue button is clicked) it pulls off the next question.

Questions Page:

<?php

session_start();
if(sizeof($_SESSION['questions_array']) > 0 )
{
    // Get the next question off of the Quiz in our SESSION
    $data = array_shift($_SESSION["questions_array"]);

    echo '<p>'.array_shift($data).'</p>'; // pop the question

    //list out the possible answers
    foreach ($data as $a => $answer) 
    {
        echo 
               '  <input type="radio" name="question-'.$q.'" id="question-'.$q.'"'.
               '         value="'.$a.'"/>'.
               '  <label for="question-'.$q.'">'.$answer.'</label>'.
               '<br>';

    }

}
else
{
  //questions array is empty, show quiz complete page
}

?>

Now your questions page only brings back one question. When submit is clicked they go to the answers page. When continue is clicked they come back. Our system then does as follows: 1) Are there questions left in our array? A)Yes A.1) Okay pop the next question with array_shift($_SESSION['questions_array']) A.2) Pop the question then list the answers. B)No B.1) No more questions, our quiz is over.

Note: There are some draw backs. If the person refreshes the page they get a new question and the other one goes unanswered. You could alternatively pass a $counter variable that updates each time the answer page loads. Then pass that counter to the $_SESSION array to bring back the next question. It's up to you.



来源:https://stackoverflow.com/questions/10236123/how-to-display-only-one-data-at-a-time-in-an-online-quiz

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!