david001's avatar

Need idea on online quiz

Hi, i am going to make online quiz. In this admin can add many questions(let say there are 100 questions) with each 4 options. This is not so hard to do

my main challenging part is in front end. Lets say there are 100 questions in db, i want to show only 10 questions for one authenticated user, one question per page with next and previous button.

so if i query like this


$question = Question::with('answers')
                    ->inRandomOrder()->first();

it still have a problem because if user do not know the answer, he may refresh page and get another question. So how do i go?? Need suggestion> Thanks

0 likes
3 replies
bobbybouwmann's avatar

You can easily solve this by adding the question ID to your session. This way whenever the user refreshes you just check if there is a question ID in the session and you return that question instead. You can also use the session to keep up the current state of the quiz.

Alternatively you can keep this state in the database as well, but that is all up to you ;)

Let me know if this works for you!

david001's avatar

Thanks @bobbybouwmann for giving idea. But still i have one questions. Lets say i'm tracking question based on session.

i have two methods first method render view: quiz method second method processQuiz: i mean save user answer in db:

In first method i need one random questions from db: lets say i do this inside quiz()method


     $question = Question::with('answers')
                      ->where('id', session('questions'))
                      ->inRandomOrder()
                      ->first();

Before that , i need to store current question id displayed on screen in session and then only i can use above method. Obviously, i can use

\Session::put(''questions','How to get current Question Id here') //HERE IS MY CONFUSION

My two methods



public function quiz(){
       
    return view('home');
}

public function processQuiz(){
    //some code to save user chosen answer in db
}

so if i use below logic

 $question = Question::with('answers')
                      ->where('id', session('questions'))
                      ->inRandomOrder()
                      ->first();

i need one question ID already stored in session

bobbybouwmann's avatar

You would probably do this

public function quiz()
{
    if (!session()->has('questions')) {
        $question = Question::with('answers')->inRandomOrder()->first();
        session->put('questions', [$firstQuestion->id]);
    } else {
        $question = Question::where('id', session()->get('questions')[0])->first();
    }

    return view('home', compact('question'));
}

public function processQuiz(Request $request) 
{
    // Save your question here
 
    $question = Question::where('id', session()->get('questions')[0])->first();
}

After that you might want to store a new question in the session or keep an array of all the questions you already had. You can then use those ids to make sure you don't get a duplicate question.

Also you can create a second session variable to keep track of the current question.

There are a lot of quiz repositories around there. Just Google for them for examples ;)

Please or to participate in this conversation.