2

The question was long, so I had to shorten it.

Anyway, I currently have the following table with the following results.

What I am doing is the following:

  1. Query all the answers associated to one question
  2. Encoding it after storing it into an array

This is my current query:

$stmt = "SELECT questions.question_text, answers.answer_text 
   FROM     questions, answers, test
   WHERE    questions.question_id = answers.question_id
   AND      questions.test_id =1";

$result = $connection->query($stmt);

Which gives me this:

enter image description here

This is the PHP:

$encode = array();

while($row = mysqli_fetch_assoc($result)) {
   $encode[] = $row;
}

echo json_encode($encode);  

Which gives me this output:

[
    {
        "question_text": "What is HTML?",
        "answer_text": "HTML is a Hypertext Markup Language"
    },
    {
        "question_text": "What is HTML?",
        "answer_text": "HTML is a Hypertext Markup Language"
    },
    {
        "question_text": "What is HTML?",
        "answer_text": "HTML is a food"
    },
    {
        "question_text": "What is HTML?",
        "answer_text": "HTML is a food"
    },
    {
        "question_text": "What is HTML?",
        "answer_text": "HTML is an Asynchronous language"
    },
    {
        "question_text": "What is HTML?",
        "answer_text": "HTML is an Asynchronous language"
    },
    {
        "question_text": "What is HTML?",
        "answer_text": "HTML is a styling language"
    },
    {
        "question_text": "What is HTML?",
        "answer_text": "HTML is a styling language"
    }
]

This is the desired output with json_encode:

"What is HTML?": {
        "1": "HTML is a Hypertext Markup Language",
        "2": "HTML is a food",
        "3": "HTML is an Asynchronous language",
        "4": "HTML is a styling language"
    }

What I am currently getting is multiple single objects with one of the answers within them but always the answer associated to it. I wish to make a single object with all of the answers in it and the question representing the object. I really hope this makes sense. I am probably way off in my logic, so please forgive me for that.

I tried playing around with the while loop but I couldn't get it to work. Can someone lead me the right way towards achieving my desired output?

Thank you.

9
  • do your questions have unique IDs ? Commented Feb 28, 2014 at 20:10
  • 3
    your desired output is neither a feature from json_encode nor mysql related functions. You have to reorganize your objects in $encode yourself. Commented Feb 28, 2014 at 20:11
  • Yes they do. @Maximus2012 Commented Feb 28, 2014 at 20:11
  • Are there multiple questions for which you want the json encoded outputs or do you want it only for 1 question ? Commented Feb 28, 2014 at 20:14
  • 1
    I think a nested database iteration. I think it would be a very logical thing to do. Based on the question ID, get all of the answers associated to it. That sounds pretty right, but I wouldn't know how to encode it. Commented Feb 28, 2014 at 20:23

4 Answers 4

9

Sounds like just altering the array you are building out...

$encode = array();

while($row = mysqli_fetch_assoc($result)) {
   $encode[$row['question _text']][] = $row['answer_text'];
}

echo json_encode($encode);
Sign up to request clarification or add additional context in comments.

7 Comments

Not exactly what OP wants but this is good enough to provide a clear approach.
tbh question was so long I skimmed :)
This gives me an error. Doesn't output anything. I believe it should. I even removed the space from "question _text" and no luck.
@TiffanyLowe what is the error message that you are getting ?
This question definitely helped me do what I wanted WITH a little tweaking from my side.
|
0

Change this bit:

$encode = array();

while($row = mysqli_fetch_assoc($result)) {
   $encode[] = $row;
}

To (start at 1 I have added the $i, instead of just pushing it to the end of the encode array):

$encode = array();
$i = 1;
while($row = mysqli_fetch_assoc($result)) {
   $encode[$row['question_text']][$i] = $row['answer_text'];
   $i++;
}

And you should be alright.

Comments

0

You do not need to use PHP to do extra processing.

Just use mysql Group By

$stmt = "SELECT questions.question_text, answers.answer_text 
  FROM     questions, answers, test
  WHERE    questions.question_id = answers.question_id
  AND      questions.test_id =1
  GROUP BY questions.question_id;"

2 Comments

This returns only one question.
Could you give me a brief explanation of your table structure
0

I changed my query to the following:

SELECT DISTINCT questions.question_text, answers.answer_text 
   FROM     questions, answers, test
   WHERE    questions.question_id = answers.question_id
   AND      questions.test_id =

The while loop to this:

while($row = mysqli_fetch_assoc($result)) {
    $encode[$row['question_text']][] = $row['answer_text'];
}

This gave me this:

{
    "What is HTML?": [
        "HTML is a Hypertext Markup Language",
        "HTML is a food",
        "HTML is an Asynchronous language",
        "HTML is a styling language"
    ]
}

Which I can now work with.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.