Eager loading from questions table, with the corresponding answers - php

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

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); });
}

How to show one array in another array in 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);
}
}

Display some parts of a file written in json

Below is my JSON file:
{
"example": "1",
"example2": 2,
"text": "3",
"info": {
"agent": 4,
"sum": 5,
"collection": [{
"Name": "6",
"Pic": "7"
} {
"Name": "8",
"Pic": "9"
}, {
"Name": "10",
"Pic": "11"
}]
}
}
How would I display each 'name' and 'pic' I think I need to use a foreach loop but don't know how to.
This is all the code I have:
$data = json_decode(file_get_contents('http://linktojson.com'));
echo $data['info']['collection'][0]['Name'];
echo $data['info']['collection'][0]['Pic'];
This should work
$data = json_decode(file_get_contents('http://linktojson.com'));
echo "<pre>".print_r($data,1)."</pre>";
foreach($data->info->collection as $key){
echo $key->Pic;
echo $key->Name;
}
Valid JSON
{
"example": "1",
"example2": 2,
"text": "3",
"info": {
"agent": 4,
"sum": 5,
"collection": [{
"Name": "6",
"Pic": "7"
}, {
"Name": "8",
"Pic": "9"
}, {
"Name": "10",
"Pic": "11"
}]
}
}

remove unnecessary hierarchy from json output

I am building an admin backend for an android app. My code provides a json output which the android developer then uses in his app. The developer asked me if it is possible to reduce the hierarchy level in the json output
as in remove the highlighted []0 and put the contents directly inside the []data instead.
This is my current php code
if($type == 1 ) //Handle item display
{
try
{
$query = "SELECT category FROM category";
$result= $DBH->query($query);
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
$cat = $row['category'];
$query1 = "SELECT * FROM item WHERE catagory='$cat'";
$value = $DBH->query($query1);
if($row1 = $value->fetchAll(PDO::FETCH_OBJ)){
$main[] = array('data'=>array($row1));
}
else
{
$main[] = array('data'=>array('catagory'=>$row['category']));
}
}
echo json_encode($main);
$result->closeCursor(); //Close database connection free resources
$DBH = null;
}
catch(PDOException $e){
print $e->getMessage ();
die();
}
}
And the json output being generated
[
{
"data": [
[
{
"id": "2",
"name": "rice",
"price": "20",
"description": "Plain Rice",
"image": "4106_rice.jpg",
"time": "12 mins",
"catagory": "Lunch",
"subcat": ""
},
{
"id": "3",
"name": "item 1",
"price": "32",
"description": "item 1 description",
"image": "1370_2Ckjikljklkljh.jpg",
"time": "10",
"catagory": "Lunch",
"subcat": "Chicken Soup"
},
{
"id": "4",
"name": "hello",
"price": "10",
"description": "fgsdjfsfsdj",
"image": "",
"time": "76",
"catagory": "Lunch",
"subcat": ""
}
]
]
},
{
"data": {
"catagory": "Dinner"
}
},
{
"data": {
"catagory": "Soup"
}
},
{
"data": {
"catagory": "Test"
}
}
]
I am not very sure if I can make the changes he asked or if it is possible. Is it doable?
Your json structure is inconsistent and try this approach, will be easier for the developer to parse
{
"response": [
{
"data": [
{
"id": "2",
"name": "rice",
"price": "20",
"description": "Plain Rice",
"image": "4106_rice.jpg",
"time": "12 mins",
"catagory": "Lunch",
"subcat": ""
},
{
"id": "3",
"name": "item 1",
"price": "32",
"description": "item 1 description",
"image": "1370_2Ckjikljklkljh.jpg",
"time": "10",
"catagory": "Lunch",
"subcat": "Chicken Soup"
},
{
"id": "4",
"name": "hello",
"price": "10",
"description": "fgsdjfsfsdj",
"image": "",
"time": "76",
"catagory": "Lunch",
"subcat": ""
}
]
},
{
"data": [
{
"catagory": "Dinner"
}
]
},
{
"data": [
{
"catagory": "Soup"
}
]
},
{
"data": [
{
"catagory": "Test"
}
]
}
]
}

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.

Categories