0

Please am trying to create an online examination platform in PHP/Laravel. I have the questions and answers stored in the database. i have been able to retrieve all Question and answer using foreach. How can I display one question/answer at a time for the student to solve. Something like a Next button after the first Question. The is my controller code:

public function showQuestions($course_code, $course_semester)
{
    $course = Course::find($course_code);

    $courseQuestion = DB::table('courses')
        ->select('*')
        ->join('questions', 'courses.course_code', 'questions.subject_code')
        ->where('course_code','=', $course_code)
        ->where('course_semester','=',$course_semester)
        ->get();

    return view('all-question-page',[
        'display' => $courseQuestion,
        'course' => $course
    ]);
}

in my blade file, I used foreach which basically displays all questions and answers and its even disallowing me to select answers for different question because it has a same ```field type name````. I want to display the Question One by One or any best possible way i can do this. I need assistance

Prince Asamoah
  • 135
  • 3
  • 11
  • why you paste two identical methods? – Andy Song Jun 16 '20 at 22:38
  • it was a mistake please.. I have updated the Query – Prince Asamoah Jun 16 '20 at 22:40
  • Don't know the format of you records in DB how those are stored, and looking at the query it looks like the database is mysql. If it is possible, rather than getting all the records at a time, get one question and answer related to that question at a time, and on next and previous button get the next and previous record. Follow the link for hint. https://stackoverflow.com/questions/21909706/laravel-previous-and-next-records/27942973 – Mukesh Joshi Jun 17 '20 at 07:08

2 Answers2

1

You can acquire your next question by adding a method on your model such as:

public function next()
{
    return static::where($this->getKeyName(), '>', $this->getKey())->first();
}

This assumes you have question context from your endpoint such as Vimona's response above. Your controller method might look like:

class QuestionController
{
    ...

    public function show($id)
    {
        // Find the question, assuming the model is Question
        $question = Question::find($id);

        return view('question', [
            'question' => $question
        ]);
    }
}

Your next button in your blade view:

<a href="{{route('question', ['id' => $question->next()->getKey()])}}">Next</a>

In summary, I would start with the first question for your course and from there you can acquire the "next" question with the next method provided.

Adam Rodriguez
  • 1,850
  • 1
  • 12
  • 15
  • Hey Adam, I clearly understand this but with my controller, I cant figure out how to grab the id of the Question. Am using the get() method here which retrieve a collection of data. ```$courseQuestion = DB::table('courses') ->select('*') ->join('questions', 'courses.course_code', 'questions.subject_code') ->where('course_code','=', $course_code) ->where('course_semester','=',$course_semester) ->get();``` – Prince Asamoah Jun 16 '20 at 23:26
  • Your Solution is working though..Will find a way to grab the ID. Thanks alot – Prince Asamoah Jun 16 '20 at 23:45
0

If you want to display the questions one by one, you can choose to display each question as a single page by using a route

/course/{course_id}/question/{question_id} 

When pressing next, you can store the student's solution and retrieve the next question by accessing the current question's course using the current question id. The course contains all question ids and you can calculate what id is next or previous.

Another way would be to use JS to change the visibility of your HTML.

Vimona
  • 347
  • 2
  • 4
  • This is my Route ```Route::get('/display/exams/{course_code}/{course_semester}','CourseController@showQuestions');``` Questions are displayed based on Course selected – Prince Asamoah Jun 16 '20 at 22:44
  • You somehow need to show the first question for the selected course. Check adam's answer. – Vimona Jun 16 '20 at 22:58