Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SMAYMAY's avatar

Next Record in a random Order

Hello everyone, im creating a quiz app for driving lessons. the quiz page have all the data but i want to do next button to take you to the next question but in in a random order.

this is my controller's function for the next but it's not working it worked with me in another project

public function submitans(Request $request) {

    $nextq = Session::get('nextq');
    $wrongans = Session::get('wrongans');
    $correctans = Session::get('correctans');

    $validate = $request->validate([
        'ans' => "required",
        'dbans' => 'required'
    ]);
    $nextq = Session::get('nextq');
    $nextq += 1;


    if ($request->dbans == $request->ans) {
        $correctans += 1;
    } else {
        $wrongans += 1;
    }

    Session::put("nextq", $nextq);
    Session::put("wrongans", $wrongans);
    Session::put("correctans", $correctans);

    $i = 0;

    $questions = question::all();

    foreach ($questions as $question) {
        $i++;
        if ($questions->count() < $nextq) {
            return view('pages.end');
        }
        if ($i == $nextq) {

            // $question = Question::where('id', '>', $question->id)->orderBy('id')->first();
            return view('pages.questions.quiz')->with(['question' => $question]);
        }
    }
}"

and this is a part of the view "

@csrf

......

  <h5 class="mt-1 ml-2">{{ $question->title }}</h5>
                        </div>
                        <div class="text-center">
                            <img src="{{ asset('storage/' . $question->image) }}" alt="image" class="rounded">
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox"  id="flexCheckDefault"
                                name="ans" checked="true" />
                            <label class="form-check-label" for="flexCheckDefault">{{ $question->reponse1 }}</label>
                        </div>

and the routing

Route::get('/quiz',[QuestionController::class,'index']); Route::any('/submitans', [QuestionController::class, 'submitans']);

0 likes
3 replies
Snapey's avatar

in a similar situation, when the student chooses to start the test, i grab the required number of questions (randomly) from the question bank, and store these in a separate table for their answers. Then the test steps through the answer records, forward and back as required. The answer contains a relationship to the original question so that the question details can be presented.

1 like

Please or to participate in this conversation.