I want to create a table question and question_option table which return the array in the format. Currently I messed up by storing the options and result in the question table
```
var quiz = {
title: 'My quiz',
questions: [
{
text: "Question 1",
responses: [
{text: 'Wrong, too bad.'},
{text: 'Right!', correct: true},
]
}, {
text: "Question 2",
responses: [
{text: 'Right answer', correct: true},
{text: 'Wrong answer'},
]
}
]
};
To retrieve data I am executing the below code.how to reorganize the table structure to get the data in the above format
```
foreach($questions as $question) {
$intermediate = array('text' => $question->question_text,'response' => array());
if($question->option1) {
if($question->correct == 1){
array_push($intermediate['response'],array('text'=> $question->option1,'correct' => true));
} else {
array_push($intermediate['response'],array('text'=> $question->option1));
}
}
if($question->option2) {
if($question->correct == 2){
array_push($intermediate['response'],array('text'=> $question->option2,'correct' => true));
} else {
array_push($intermediate['response'],array('text'=> $question->option2));
}
}
if($question->option3) {
if($question->correct == 3){
array_push($intermediate['response'],array('text'=> $question->option3,'correct' => true));
} else {
array_push($intermediate['response'],array('text'=> $question->option3));
}
}
if($question->option4) {
if($question->correct == 4){
array_push($intermediate['response'],array('text'=> $question->option4,'correct' => true));
} else {
array_push($intermediate['response'],array('text'=> $question->option4));
}
}
if($question->option5) {
if($question->correct == 5){
array_push($intermediate['response'],array('text'=> $question->option5,'correct' => true));
} else {
array_push($intermediate['response'],array('text'=> $question->option5));
}
}
array_push($result,$intermediate);
}
return $result;
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community