How to show one array in another array in php? - php

I have table of a survey,answer and user. Answer and survey table is linked by user_id also user and survey is linked with user_id.
I want to show answers and user and comment for a survey. comment is inside survey table.
I want to get the result in this format:
{
result:1,
message:'success',
survey:[
{
answers:
[
],
user:
{
}
comment:
},
{
answers:
[
],
user:
{
}
comment:
}
]
}
getAllSurveyByDoctor function:
function getAllSurveyByDoctor($user_id)
{
$database = new SurveyDatabase(SurveyConstants::DBHOST,SurveyConstants::DBUSER,SurveyConstants::DBPASS,SurveyConstants::DBNAME);
$dbConnection = $database->getDB();
$stmt = $dbConnection->prepare("SELECT * from survey where `user_id` = ?");
$stmt->execute(array($user_id));
$survey = $stmt->fetchAll(PDO::FETCH_ASSOC);
$surveys = array();
$answers = array();
if (count($survey) > 0) {
foreach($survey as $row)
{
$stmt = $dbConnection->prepare("SELECT answer.answer_id, answer.survey_id, answer.question_id, answer.rating, answer.user_id, question.question_id, question.question, question.type FROM `answer` INNER JOIN `question` ON
answer.question_id = question.question_id WHERE answer.user_id = ? and answer.survey_id = ?");
$s_id = $row['survey_id'];
$stmt->execute(array($user_id,$s_id));
$answer = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt = $dbConnection->prepare("SELECT users.user_id,users.email_id,users.pass,users.address,users.name,survey.survey_id,survey.user_id FROM `users` INNER JOIN `survey` ON users.user_id = survey.user_id WHERE users.user_id = ?");
$stmt->execute(array($user_id));
$user = $stmt->fetch(PDO::FETCH_ASSOC);
$comment = $row['comment'];
// $surveys[] = $row;
$answers = $answer;
$surveys[] = $answers;
$surveys[] = $user;
$surveys[] = $comment;
}
$response = array("status" => 1, "message" => "Success", "surveys" => $surveys);
return json_encode($response);
}
else {
$response = array("status"=>-1,"message"=>"surveys list is empty");
return json_encode($response);
}
}
I get the result like this:
{
"status": 1,
"message": "Success",
"surveys": [
[
{
"answer_id": "50",
"survey_id": "35",
"question_id": "1",
"rating": "4",
"user_id": "9",
"question": "Scheduling appointments",
"type": "rating"
},
{
"answer_id": "51",
"survey_id": "35",
"question_id": "2",
"rating": "6",
"user_id": "9",
"question": "Office environment",
"type": "rating"
}
],
{
"user_id": "9",
"email_id": "user7#gmail.com",
"pass": "user7",
"address": "miraroad",
"name": "user7",
"survey_id": "35"
},
"0",
[],
{
"user_id": "9",
"email_id": "user7#gmail.com",
"pass": "user7",
"address": "miraroad",
"name": "user7",
"survey_id": "35"
},
"0",
[
{
"answer_id": "54",
"survey_id": "37",
"question_id": "1",
"rating": "4",
"user_id": "9",
"question": "Scheduling appointments",
"type": "rating"
},
{
"answer_id": "55",
"survey_id": "37",
"question_id": "2",
"rating": "6",
"user_id": "9",
"question": "Office environment",
"type": "rating"
}
],
{
"user_id": "9",
"email_id": "user7#gmail.com",
"pass": "user7",
"address": "miraroad",
"name": "user7",
"survey_id": "35"
},
"0",
[],
{
"user_id": "9",
"email_id": "user7#gmail.com",
"pass": "user7",
"address": "miraroad",
"name": "user7",
"survey_id": "35"
},
"csdcs",
[
{
"answer_id": "72",
"survey_id": "49",
"question_id": "7",
"rating": "5",
"user_id": "9",
"question": "Doctor listens and answers questions",
"type": "rating"
},
{
"answer_id": "73",
"survey_id": "49",
"question_id": "6",
"rating": "5",
"user_id": "9",
"question": "Doctor explains medical condition(s)",
"type": "rating"
}
],
{
"user_id": "9",
"email_id": "user7#gmail.com",
"pass": "user7",
"address": "miraroad",
"name": "user7",
"survey_id": "35"
},
"",
[
{
"answer_id": "74",
"survey_id": "50",
"question_id": "8",
"rating": "5",
"user_id": "9",
"question": "Doctor spends enough time with patients",
"type": "rating"
},
{
"answer_id": "75",
"survey_id": "50",
"question_id": "3",
"rating": "5",
"user_id": "9",
"question": "Staff helpfulness",
"type": "rating"
}
],
{
"user_id": "9",
"email_id": "user7#gmail.com",
"pass": "user7",
"address": "miraroad",
"name": "user7",
"survey_id": "35"
},
"",
[
{
"answer_id": "76",
"survey_id": "51",
"question_id": "7",
"rating": "5",
"user_id": "9",
"question": "Doctor listens and answers questions",
"type": "rating"
},
{
"answer_id": "77",
"survey_id": "51",
"question_id": "6",
"rating": "5",
"user_id": "9",
"question": "Doctor explains medical condition(s)",
"type": "rating"
}
],
{
"user_id": "9",
"email_id": "user7#gmail.com",
"pass": "user7",
"address": "miraroad",
"name": "user7",
"survey_id": "35"
},
"",
[
{
"answer_id": "78",
"survey_id": "52",
"question_id": "8",
"rating": "5",
"user_id": "9",
"question": "Doctor spends enough time with patients",
"type": "rating"
},
{
"answer_id": "79",
"survey_id": "52",
"question_id": "5",
"rating": "5",
"user_id": "9",
"question": "Trust in doctor's decision",
"type": "rating"
}
],
{
"user_id": "9",
"email_id": "user7#gmail.com",
"pass": "user7",
"address": "miraroad",
"name": "user7",
"survey_id": "35"
},
"",
[
{
"answer_id": "80",
"survey_id": "53",
"question_id": "1",
"rating": "5",
"user_id": "9",
"question": "Scheduling appointments",
"type": "rating"
},
{
"answer_id": "81",
"survey_id": "53",
"question_id": "7",
"rating": "5",
"user_id": "9",
"question": "Doctor listens and answers questions",
"type": "rating"
}
],
{
"user_id": "9",
"email_id": "user7#gmail.com",
"pass": "user7",
"address": "miraroad",
"name": "user7",
"survey_id": "35"
},
"",
[
{
"answer_id": "82",
"survey_id": "54",
"question_id": "6",
"rating": "5",
"user_id": "9",
"question": "Doctor explains medical condition(s)",
"type": "rating"
},
{
"answer_id": "83",
"survey_id": "54",
"question_id": "3",
"rating": "5",
"user_id": "9",
"question": "Staff helpfulness",
"type": "rating"
}
],
{
"user_id": "9",
"email_id": "user7#gmail.com",
"pass": "user7",
"address": "miraroad",
"name": "user7",
"survey_id": "35"
},
""
]
}
I am getting some what format same as I want. I am getting answers array, user and comment after that. But how I can name it? Can not identify from this result that where are answers, user and comment. I want to name it. How can I? Thank you.

You can simply create an associative array for each survey. Inside this associative array, assign your keys and your values. Here's how it looks :
function getAllSurveyByDoctor($user_id)
{
$database = new SurveyDatabase(SurveyConstants::DBHOST,SurveyConstants::DBUSER,SurveyConstants::DBPASS,SurveyConstants::DBNAME);
$dbConnection = $database->getDB();
$stmt = $dbConnection->prepare("SELECT * from survey where `user_id` = ?");
$stmt->execute(array($user_id));
$survey = $stmt->fetchAll(PDO::FETCH_ASSOC);
$surveys = array();
$answers = array();
if (count($survey) > 0) {
foreach($survey as $row)
{
$stmt = $dbConnection->prepare("SELECT answer.answer_id, answer.survey_id, answer.question_id, answer.rating, answer.user_id, question.question_id, question.question, question.type FROM `answer` INNER JOIN `question` ON
answer.question_id = question.question_id WHERE answer.user_id = ? and answer.survey_id = ?");
$s_id = $row['survey_id'];
$stmt->execute(array($user_id,$s_id));
$answer = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt = $dbConnection->prepare("SELECT users.user_id,users.email_id,users.pass,users.address,users.name,survey.survey_id,survey.user_id FROM `users` INNER JOIN `survey` ON users.user_id = survey.user_id WHERE users.user_id = ?");
$stmt->execute(array($user_id));
$user = $stmt->fetch(PDO::FETCH_ASSOC);
$comment = $row['comment'];
// $surveys[] = $row;
//We create the "survey" associative array
$survey = [];
$answers = $answer;
$survey['answers']= $answers;
$survey['user'] = $user;
$survey['comment']=$comment;
//We add the survey array to the survey
$surveys[] = $survey;
}
$response = array("status" => 1, "message" => "Success", "surveys" => $surveys);
return json_encode($response);
}
else {
$response = array("status"=>-1,"message"=>"surveys list is empty");
return json_encode($response);
}
}

Related

Sorting only a part of object/array in PHP

I've been struggling around this for a while now and can't seem to get the right algorithm.
What I want is to sort this:
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}],
"position": "0"
}
}';
And the output would be
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
},{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}, {
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}],
"position": "0"
}
}';
So basically it should be sorted using the label inside the "options" without affecting the other fields. I've been trying usort and tried different algorithms but to no avail.
Current code that sorts only the outside "label" and not the inner label:
function optionsSort($a, $b) {
return strcmp($a['options'][0]['label'], $b['options'][0]['options']['label']);
}
usort($variable, "optionsSort"));
Here's the code:
// your array
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}],
"position": "0"
}
}';
$a = json_decode($variable);
foreach ($a as $item) {
// sorting function
usort($item->options, function($a, $b) { return strcmp($a->label, $b->label); });
}

Obtaining the last level of a json tree on php

I have a json tree with categories,
I would like to obtain the last level of elements for each different category.
For example on this json:
[
{
"category_id": "3",
"parent_id": "2",
"name": "Women",
"categories": [
{
"category_id": "11",
"parent_id": "3",
"name": "Clothing",
"categories": [
{
"category_id": "30",
"parent_id": "11",
"name": "T-shirts",
"categories": null
},
{
"category_id": "33",
"parent_id": "11",
"name": "jeans",
"categories": null
}
]
}
]
},
{
"category_id": "5",
"parent_id": "2",
"name": "Footwear ",
"categories": [
{
"category_id": "15",
"parent_id": "5",
"name": "Rings",
"categories": [
{
"category_id": "51",
"parent_id": "15",
"name": "Small Leathers",
"categories": null
}
]
},
{
"category_id": "16",
"parent_id": "5",
"name": "Bands",
"categories": [
{
"category_id": "41",
"parent_id": "16",
"name": "boots",
"categories": null
}
]
},
{
"category_id": "48",
"parent_id": "5",
"name": "Bracelets",
"categories": [
{
"category_id": "55",
"parent_id": "48",
"name": "Cocktail",
"categories": null
}
]
}
]
}
]
The result would be an array (T-shirts, Jeans, Small Leathers, boots, cocktail)
What I was thinking is to decode it on an array and search filter the array with all the categories that are null, but I'm not sure if it's the best option because the object have different levels.
(I'm sorry for the English)
Json string is not a valid. It can be made valid by enclosing the json string in {}.
$json = '{"categories": [
{
"category_id": "3",
"parent_id": "2",
"name": "Women",
"categories": [
{
"category_id": "11",
"parent_id": "3",
"name": "Clothing",
"categories": [
{
"category_id": "30",
"parent_id": "11",
"name": "T-shirts",
"categories": null
},
{
"category_id": "33",
"parent_id": "11",
"name": "jeans",
"categories": null
}
]
}
]
},
{
"category_id": "5",
"parent_id": "2",
"name": "Footwear ",
"categories": [
{
"category_id": "15",
"parent_id": "5",
"name": "Rings",
"categories": [
{
"category_id": "51",
"parent_id": "15",
"name": "Small Leathers",
"categories": null
}
]
},
{
"category_id": "16",
"parent_id": "5",
"name": "Bands",
"categories": [
{
"category_id": "41",
"parent_id": "16",
"name": "boots",
"categories": null
}
]
},
{
"category_id": "48",
"parent_id": "5",
"name": "Bracelets",
"categories": [
{
"category_id": "55",
"parent_id": "48",
"name": "Cocktail",
"categories": null
}
]
}
]
}
]}';
Decode the json string in to php opject
$json_object = json_decode($json);
Use this recursive function to fetch get all the last level categories in a array.
function getLastCategories($object){
$last_categories = array();
if(is_array($object->categories)){
foreach($object->categories as $categories){
$last_categories = array_merge( $last_categories, getLastCategories($categories));
}
}else{
$last_categories[]=$object->name;
}
return $last_categories;
}
print_r(getLastCategories($json_object));
Output:
Array
(
[0] => T-shirts
[1] => jeans
[2] => Small Leathers
[3] => boots
[4] => Cocktail
)
You can do this using recursive iterators.
$categories = json_decode($json);
// create the iterator
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($categories));
// iterate recursively
foreach ($iterator as $key => $subcategories) {
// if the 'categories' key is empty for the current level...
if ($key == 'categories' && !$subcategories) {
// then add the value of the 'name' key for the current level to your result array.
$result[] = $iterator->getSubIterator()->offsetGet('name');
}
}
This may not be the most efficient way to do it, but it makes the code pretty simple.

PHP - Foreach variable works only if read

I am trying to read posts under each category as shown below. There are multiple categories with id "4", "3", "2", "1". Only for category "1" there is a post available.
foreach ($wp_categories as $wp_category) {
$id = $wp_category->term_taxonomy_id;
//if($id == "1") {
$posts = $json_api->introspector->get_posts(array(
'cat' => $id
));
//}
$result[] = $posts;
}
return $result;
In the above code if I just uncomment the if condition, I am getting the posts in the result array. But if this is commented I am not getting the posts in result.
I am new to PHP and above condition is not making any sense. It would be of great help if someone could explain why having the if condition is making the post available in the result.
dumping $wp_categories below
{
"status": "ok",
"0": {
"term_id": "5",
"name": "Category 5",
"slug": "category5",
"term_group": "0",
"term_order": "1",
"term_taxonomy_id": "5",
"taxonomy": "category",
"description": "",
"parent": "0",
"count": "0"
},
"1": {
"term_id": "2",
"name": "Category 4",
"slug": "category4",
"term_group": "0",
"term_order": "2",
"term_taxonomy_id": "2",
"taxonomy": "category",
"description": "",
"parent": "0",
"count": "0"
},
"2": {
"term_id": "3",
"name": "Category 3",
"slug": "category3",
"term_group": "0",
"term_order": "3",
"term_taxonomy_id": "3",
"taxonomy": "category",
"description": "",
"parent": "0",
"count": "0"
},
"3": {
"term_id": "4",
"name": "abc",
"slug": "abc",
"term_group": "0",
"term_order": "4",
"term_taxonomy_id": "4",
"taxonomy": "category",
"description": "",
"parent": "0",
"count": "0"
},
"4": {
"term_id": "1",
"name": "Others",
"slug": "others",
"term_group": "0",
"term_order": "5",
"term_taxonomy_id": "1",
"taxonomy": "category",
"description": "",
"parent": "0",
"count": "1"
}
}
This is simply shows that it will be true only in case of id 1:
if($id == "1") {
I think this should be:
if(isset($id) && $id != '') {
to get the all the post.

php JSON data structure for angular app

I am trying to format the a mysql response into a array of objects so that Angular can easily traverse the data.
mysql result:
[{
"FIAccountsID": "99",
"AccountName": "",
"AccountCustomName": "Brothers",
"AccountNumber": "99-123123123",
"AccountTypeName": "IRA",
"FinancialInstName": "Testes",
"FinancialInstID": "9",
"UserID": "1",
"ofxStatusCode": "500",
"UserAccountID": "09128-vc-12",
"FirstName": "Someones",
"LastName": "Name",
"Status": "1"
},
{
"FIAccountsID": "99",
"AccountName": "",
"AccountCustomName": "Brothers",
"AccountNumber": "99-123123123",
"AccountTypeName": "IRA",
"FinancialInstName": "Testes",
"FinancialInstID": "9",
"UserID": "1",
"ofxStatusCode": "500",
"UserAccountID": "09128-vc-12",
"FirstName": "Someones",
"LastName": "Name",
"Status": "2"
},
{
"FIAccountsID": "100",
"AccountName": "",
"AccountCustomName": "Brothers",
"AccountNumber": "99-123123123",
"AccountTypeName": "IRA",
"FinancialInstName": "Testes",
"FinancialInstID": "9",
"UserID": "1",
"ofxStatusCode": "500",
"UserAccountID": "09128-vc-12",
"FirstName": "Someones",
"LastName": "Name",
"Status": "1"
},
{
"FIAccountsID": "100",
"AccountName": "",
"AccountCustomName": "Brothers",
"AccountNumber": "99-123123123",
"AccountTypeName": "IRA",
"FinancialInstName": "Testes",
"FinancialInstID": "9",
"UserID": "1",
"ofxStatusCode": "500",
"UserAccountID": "09128-vc-12",
"FirstName": "Someones",
"LastName": "Name",
"Status": "2"
},
{
"FIAccountsID": "101",
"AccountName": "",
"AccountCustomName": "Brothers",
"AccountNumber": "99-123123123",
"AccountTypeName": "IRA",
"FinancialInstName": "Testes",
"FinancialInstID": "9",
"UserID": "1",
"ofxStatusCode": "500",
"UserAccountID": "09128-vc-12",
"FirstName": "Someones",
"LastName": "Name",
"Status": "1"
},
{
"FIAccountsID": "101",
"AccountName": "",
"AccountCustomName": "Brothers",
"AccountNumber": "99-123123123",
"AccountTypeName": "IRA",
"FinancialInstName": "Testes",
"FinancialInstID": "9",
"UserID": "1",
"ofxStatusCode": "500",
"UserAccountID": "09128-vc-12",
"FirstName": "Someones",
"LastName": "Name",
"Status": "2"
},
{
"FIAccountsID": "102",
"AccountName": "",
"AccountCustomName": "Brothers",
"AccountNumber": "99-123123123",
"AccountTypeName": "IRA",
"FinancialInstName": "Testes",
"FinancialInstID": "9",
"UserID": "1",
"ofxStatusCode": "500",
"UserAccountID": "09128-vc-12",
"FirstName": "Someones",
"LastName": "Name",
"Status": "1"
},
{
"FIAccountsID": "102",
"AccountName": "",
"AccountCustomName": "Brothers",
"AccountNumber": "99-123123123",
"AccountTypeName": "IRA",
"FinancialInstName": "Testes",
"FinancialInstID": "9",
"UserID": "1",
"ofxStatusCode": "500",
"UserAccountID": "09128-vc-12",
"FirstName": "Someones",
"LastName": "Name",
"Status": "2"
},
{
"FIAccountsID": "103",
"AccountName": "",
"AccountCustomName": "Brothers",
"AccountNumber": "99-123123123",
"AccountTypeName": "IRA",
"FinancialInstName": "Testes",
"FinancialInstID": "9",
"UserID": "1",
"ofxStatusCode": "500",
"UserAccountID": "09128-vc-12",
"FirstName": "Someones",
"LastName": "Name",
"Status": "1"
},
{
"FIAccountsID": "103",
"AccountName": "",
"AccountCustomName": "Brothers",
"AccountNumber": "99-123123123",
"AccountTypeName": "IRA",
"FinancialInstName": "Testes",
"FinancialInstID": "9",
"UserID": "1",
"ofxStatusCode": "500",
"UserAccountID": "09128-vc-12",
"FirstName": "Someones",
"LastName": "Name",
"Status": "2"
}......
here it the PHP:
while($row = mysqli_fetch_assoc($result))
{
$uid = $row['UserID'];
$name = $row['FirstName'].' '.$row['LastName'];
$fiid = $row['FinancialInstID'];
$fi = $row['FinancialInstName'];
$acctID = $row['FIAccountsID'];
$rows[$uid]['name'] = $name;
$rows[$uid]['uid'] = $uid;
$rows[$uid]['fi'][$fiid]['name'] = $fi;
$rows[$uid]['fi'][$fiid]['acct'][$acctID]['name'] = $row['AccountCustomName'];
}
print json_encode($rows);
this is what i get:
{
"1": {
"name": "Some Name",
"uid": "1",
"fi": {
"9": {
"name": "Testes",
"accts": {
"99": {
"name": "name 1"
},
"100": {
"name": "name 2"
},
"103": {
"name": "name 3"
}
}
}
}
},
"2": {
"name": "Another Name",
"uid": "2",
"fi": {
"7": {
"name": "Trevor's Brokerage House",
"accts": {
"1": {
"name": "Sally's 401k Account"
},
"2": {
"name": "retirement"
},
"3": {
"name": "Some other account"
..........
desired result:
[
{
"users": [
{
"name": "Some Name",
"uid": "1",
"fi": [
{
"name": "Testes",
"acct": [
{
"name": "name 1"
},
{
"name": "name 2"
},
{
"name": "name 3"
}
]
}
]
},
{
"name": "Another Name",
"uid": "2",
"fi": [
{
"name": "Trevor's Brokerage House",
"acct": [
{
"name": "Sally's 401k Account"
},
{
"name": "retirement"
},
{
"name": "Some other account"
..........
I want to know how to have the data structure without the unique keys prefixing the nested arrays
The thing is that if you define keys, json_encode will read them. If you don't want keys, declare the array without them. So, what you need to do is to structure your array according to you desired output.
But the way your data is arranged make it somewhat hard to format it properly. The solution I can see is to do what you're already doing:
while($row = mysqli_fetch_assoc($result))
{
$uid = $row['UserID'];
$name = $row['FirstName'].' '.$row['LastName'];
$fiid = $row['FinancialInstID'];
$fi = $row['FinancialInstName'];
$acctID = $row['FIAccountsID'];
$rows[$uid]['name'] = $name;
$rows[$uid]['uid'] = $uid;
$rows[$uid]['fi'][$fiid]['name'] = $fi;
$rows[$uid]['fi'][$fiid]['acct'][$acctID]['name'] = $row['AccountCustomName'];
}
But then you revisit the array and remove all the keys using array_values:
foreach ($rows as $uid => $data)
{
foreach ($data['fi'] as $fiid => $fi)
{
$rows[$uid]['fi'][$fiid]['acct'] = array_values($fi['acct']);
}
$rows[$uid]['fi'] = array_values($rows[$uid]['fi']);
}
// Here you format the outer array:
$rows = array(array('users' => array_values($rows)));
Which gives a result like this:
[
{
"users": [
{
"name": "Someones Name",
"uid": "1",
"fi": [
{
"name": "Testes",
"acct": [
{
"name": "Brothers"
},
{
"name": "Brothers"
},
{
"name": "Brothers"
},
{
"name": "Brothers"
},
{
"name": "Brothers"
}
]
}
]
},
{
"name": "Someones Name",
"uid": "2",
"fi": [
{
"name": "Testes",
"acct": [
{
"name": "Brothers"
},
{
"name": "Brothers"
}
]
}
]
}
]
}
]

Eager loading from questions table, with the corresponding answers

Im trying to figure out how i can fetch "questions" from my questions table, and at the same time fetch the corresponding answers, or in my specific case I have called them choices.
Currently I´m fetching it like this:
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();
}
So I´m doing two (2) queries, first I'm fetching the question, then I'm fetching the question choices. The result in JSON format is this:
{
"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"
}
]
}
But he "correct" way should probably be that the answers (choices) is nested inside the question:
{
"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"
}
]
}
}
My question:
How do I join (or eager load) the choices to the question?
You could write a 3rd function that pulls both using a join.
$sql = "SELECT
q.id as question_id, q.content, q.source, q.image_url, q.lastmodified, q.quiz_id,
c.id AS answer_id, c.content as an_content, c.correct, c.question_id
FROM questions as q
INNER JOIN choices AS c ON q.id = c.question_id
WHERE q.id = :id";
$database = DatabaseFactory::getFactory()->getConnection();
$question = Self::getFraga($id);
$answer = Self::getAnswers($id);
$question->answer = $answer;
return $question;
Produces:
{
"question": {
"id": "12",
"content": "asdasd",
"source": "asd",
"image_url": "156ebc3206212c_qijpmnklohgfe.jpeg",
"lastmodified": "2016-03-18 09:58:08",
"quiz_id": "6",
"answer": [
{
"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"
}
]
}
}
Very simpel solution after all

Categories