0
我試圖弄清楚我如何從我的questions
表中獲取「問題」,同時獲取相應的answers
,或者在我的具體情況下,我稱他們爲選擇。從問題表急切加載,相應的答案
目前I'm獲取這樣的:
public static function getQuestion($id)
{
$sql = "SELECT * FROM questions WHERE id = :id";
$query = $database->prepare($sql);
$query->execute(array(':id' => $id));
if($query->rowCount() == 1){
return $query->fetch();
}
return false;
}
public static function getChoices($id)
{
$sql = "SELECT * FROM choices WHERE question_id = :id";
$query = $database->prepare($sql);
$query->execute(array(':id' => $id));
return $query->fetchAll();
}
所以I'm做兩(2)查詢,首先我取這個問題,那麼我取這個問題的選擇。 JSON格式的結果是這樣的:
{
"question": {
"id": "12",
"content": "asdasd",
"source": "asd",
"image_url": "156ebc3206212c_qijpmnklohgfe.jpeg",
"lastmodified": "2016-03-18 09:58:08",
"quiz_id": "6"
},
"answers": [
{
"id": "45",
"content": "Ja",
"correct": "0",
"question_id": "12"
},
{
"id": "46",
"content": "nej",
"correct": "0",
"question_id": "12"
},
{
"id": "47",
"content": "inte",
"correct": "0",
"question_id": "12"
},
{
"id": "48",
"content": "kanske ",
"correct": "1",
"question_id": "12"
}
]
}
但他「正確」的方式或許應該是答案(選擇)嵌套在問題裏面:
{
"question": {
"id": "12",
"content": "asdasd",
"source": "asd",
"image_url": "156ebc3206212c_qijpmnklohgfe.jpeg",
"lastmodified": "2016-03-18 09:58:08",
"quiz_id": "6",
"answers": [
{
"id": "45",
"content": "Ja",
"correct": "0",
"question_id": "12"
},
{
"id": "46",
"content": "nej",
"correct": "0",
"question_id": "12"
},
{
"id": "47",
"content": "inte",
"correct": "0",
"question_id": "12"
},
{
"id": "48",
"content": "kanske ",
"correct": "1",
"question_id": "12"
}
]
}
}
我的問題:
我如何加入(或急切地加載)這個問題的選擇?
後非常simpel的解決方案,並沒有以預期的方式加載數據。 ' 「問題」:[{ 「ID」: 「45」, 「內容」: 「JA」, 「源」: 「ASD」, 「IMAGE_URL」: 「156ebc3206212c_qijpmnklohgfe.jpeg」, 「上次更改時間「:」2016-03-18 09:58:08「,」 「question_id」:「12」 }, – Adam