我有调查表,答案和用户。答案和调查表由user_id和用户链接,调查与user_id相关联。
我想为调查显示答案,用户和评论。评论在调查表内。
我想以这种格式得到结果:
{
result:1,
message:'success',
survey:[
{
answers:
[
],
user:
{
}
comment:
},
{
answers:
[
],
user:
{
}
comment:
}
]
}
getAllSurveyByDoctor功能:
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);
}
}
我得到的结果如下:
{
"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"
},
""
]
}
我得到了一些我想要的格式。之后我得到答案数组,用户和评论。但我怎么命名呢?无法从此结果中识别出答案,用户和评论的位置。我想说出来。我怎么能够?谢谢。
答案 0 :(得分:3)
您可以简单地为每个调查创建一个关联数组。在此关联数组中,指定您的键和值。这是它的外观:
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);
}
}