0

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": "[email protected]",
      "pass": "user7",
      "address": "miraroad",
      "name": "user7",
      "survey_id": "35"
    },
    "0",
    [],
    {
      "user_id": "9",
      "email_id": "[email protected]",
      "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": "[email protected]",
      "pass": "user7",
      "address": "miraroad",
      "name": "user7",
      "survey_id": "35"
    },
    "0",
    [],
    {
      "user_id": "9",
      "email_id": "[email protected]",
      "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": "[email protected]",
      "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": "[email protected]",
      "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": "[email protected]",
      "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": "[email protected]",
      "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": "[email protected]",
      "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": "[email protected]",
      "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.

2
  • $surveys[] = is just sequentially adding a new value to the array. If you want to name it. Use a key counter. Like $surveys[$i]['user_id'] = $user, $surveys[$i]['comment'] = $comment. Make sure to increase the counter. Commented Sep 13, 2016 at 18:19
  • Alexis' solution works as well. A way to do it without the need of a counter. Commented Sep 13, 2016 at 18:21

1 Answer 1

3

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);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.