[英]Cant Getting multiple rows with same foreign key in SQL using codeigniter 3
I've created a web app using code igniter 3 to get data from 3 tables and display them in the view (quiz_table,question_table and answers_table).我已经使用 code igniter 3 创建了一个网络应用程序,以从 3 个表中获取数据并将它们显示在视图中(quiz_table、question_table 和 answers_table)。 The application is a quiz app(MCQ quiz).
该应用程序是一个测验应用程序(MCQ 测验)。
A single quiz contains 4 questions and each question contains 4 answers where only one is correct.一个测验包含 4 个问题,每个问题包含 4 个答案,其中只有一个是正确的。
I wrote a SQL query to retrieve data from all 3 tables such as quiz table, creator name, rating, question id, question title, correct answer,and the list of answers for that question.我编写了一个 SQL 查询来从所有 3 个表中检索数据,例如测验表、创建者姓名、评分、问题 ID、问题标题、正确答案以及该问题的答案列表。 I'm trying to reference the question Id of the answers table and get all 4 answers to a question.
我正在尝试引用答案表的问题 ID 并获取问题的所有 4 个答案。
Below is the model code,下面是模型代码,
function getSingleQuizQuestionDataFromDB($quizId)
{ //insert query
try {
$this->db->select('quiz_table.quizName');
$this->db->select('quiz_table.creatorName');
$this->db->select('quiz_table.rating');
$this->db->select('question_table.questionId');
$this->db->select('question_table.questionTitle');
$this->db->select('question_table.correctAnswer');
$this->db->select('answer_table.answer');
$this->db->from('quiz_table');
$this->db->where('quiz_table.quizId',$quizId);
$this->db->join('question_table','question_table.quizId = quiz_table.quizId','INNER');
$this->db->join('answer_table','answer_table.questionId= question_table.questionId');
$this->db->group_by(['quiz_table.quizId', 'question_table.questionId']);
$result = $this->db->get();
$singleQuizQuestionData= $result->result_array();
return $singleQuizQuestionData;
} catch (Exception $e) {
// log_message('error: ',$e->getMessage());
return;
}
}
Below is the result I get from this query,下面是我从这个查询中得到的结果,
{
"singleQuizQuestionData": [
{
"quizName": "quiz 1",
"creatorName": "kavibebi",
"rating": "0",
"questionId": "76",
"questionTitle": "q1q1",
"correctAnswer": "q1a",
"answer": "q1q1a1"
},
{
"quizName": "quiz 1",
"creatorName": "kavibebi",
"rating": "0",
"questionId": "77",
"questionTitle": "q1q2",
"correctAnswer": "q1a",
"answer": "q1q2a1"
},
{
"quizName": "quiz 1",
"creatorName": "kavibebi",
"rating": "0",
"questionId": "78",
"questionTitle": "q1q3",
"correctAnswer": "q1a",
"answer": "q1q3a1"
},
{
"quizName": "quiz 1",
"creatorName": "kavibebi",
"rating": "0",
"questionId": "79",
"questionTitle": "q1q4",
"correctAnswer": "q1a",
"answer": "q1q4a1"
}
]
}
As you can see above, this query only returns the first answer from each question.How can I get all 4 answers of a question in the response?正如您在上面看到的,此查询仅返回每个问题的第一个答案。如何在响应中获得一个问题的所有 4 个答案? Please help!
请帮忙!
$singleQuizData = array();
foreach ($singleQuizQuestionData as $row) {
// If this is the first row for a new question, create a new array for the
// question
$singleQuizData[$row['questionId']] = array(
'quizName' => $row['quizName'],
'creatorName' => $row['creatorName'],
'rating' => $row['rating'],
'questionId' => $row['questionId'],
'questionTitle' => $row['questionTitle'],
'correctAnswer' => $row['correctAnswer'],
'answers' => $row['answer'],
);
}
// The $singleQuizData array should now contain one element for each question
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.