What I want:
- To get the
grade
of what the user answered in the database if exists and get the grade of that answer.
What I have:
- I have
3
tables,short_answer
,sa_sa_answer
,short_answer_answer
. - Now,
short_answer
table has question, and each question has many answer located in theshort_answer_answer
table and the grade is also included there, the question can have 1 or more answer.
What code I have:
Controller
foreach($sa_question_id as $key => $value){
$sa = ShortAnswer::with('answers')->find($sa_question_id[$key]);
$possible_answers = [];
foreach($sa->answers as $possible_answer){
$possible_answers[] .= strtolower($possible_answer->answer);
}
if(in_array(strtolower($sa_answer[$key]), $possible_answers)){
$grade = ShortAnswerAnswer::where('answer', $sa_answer[$key])->get();
echo "plus +1. Grade is: " . $grade->grade . "<br>";
}
}
The problem is:
Im just getting the answer where the answer is equal to the user's answer. But what if I have TWO same answer and different grade
and obviously different question. It can select the wrong one.
Note: I'm using Laravel5.1
Update: Table Structure
short_answer
- name
- question
sa_sa_answer
- short_answer_id
- short_answer_answer_id
short_answer_answer
- answer
- grade
Update
I've already solved this issue, however no one got the bounty, but If you could answer this question, I can give you the bounty plus 2 check mark and up vote, I just really need more help with this. It is also connected with this question. The bounty will be gone in 3 days from now.