0

I have MySQL table named qbank having fields - qid, question_text, subject_id, option_01_text, option_01_is_correct, option_02_text, option_02_is_correct, option_03_text, option_03_is_correct, option_04_text, option_04_is_correct.

My desired JSON output is like this -

[{
        "qid": 5788,
        "question_text": "Which of the following is a traction epiphysis ?",
        "subject_id": 2,
        "options": [{
                "option_code": "A",
                "option_text": "Tibial condyles",
                "option_is_correct": 0
            },
            {
                "option_code": "B",
                "option_text": "Trochanter of femur",
                "option_is_correct": 1
            },
            {
                "option_code": "C",
                "option_text": "Coracoid process of scapula",
                "option_is_correct": 0
            },
            {
                "option_code": "D",
                "option_text": "head of femur",
                "option_is_correct": 0
            }
        ]
    },
    {
        "qid": 5789,
        "question_text": "Which is the most prominent spinous process",
        "subject_id": 2,
        "options": [{
                "option_code": "A",
                "option_text": "T1",
                "option_is_correct": 0
            },
            {
                "option_code": "B",
                "option_text": "C7",
                "option_is_correct": 1
            },
            {
                "option_code": "C",
                "option_text": "C6",
                "option_is_correct": 0
            },
            {
                "option_code": "D",
                "option_text": "L5",
                "option_is_correct": 0
            }
        ]
    }
]

My PHP code is -

$sql = "SELECT * FROM qbank Limit 2";
$query  = $mysqli->prepare($sql);
$query->execute();
$result = $query->get_result();

$output = [];
$options = [];

while ($row = $result->fetch_assoc()) {
    $output[] = [
        "qid"              => $row["qid"],
        "question_text"    => $row["question_text"],
        "subject_id"      => $row["subject_id"],
    ];

    $options[] = [
        "option_code" => "A",
        "option_text" => $row["option_01_text"],
        "option_is_correct" => $row["option_01_is_correct"],
    ];
    $options[] = [
        "option_code" => "B",
        "option_text" => $row["option_02_text"],
        "option_is_correct" => $row["option_02_is_correct"],
    ];
    $options[] = [
        "option_code" => "C",
        "option_text" => $row["option_03_text"],
        "option_is_correct" => $row["option_03_is_correct"],
    ];
    $options[] = [
        "option_code" => "D",
        "option_text" => $row["option_04_text"],
        "option_is_correct" => $row["option_04_is_correct"],
    ];
}
echo json_encode($output, JSON_PRETTY_PRINT);
echo json_encode($options, JSON_PRETTY_PRINT);

The output I am getting from PHP is -

[
    {
        "qid": 5788,
        "question_text": "Which of the following is a traction epiphysis ?",
        "subject_id": 2
    },
    {
        "qid": 5789,
        "question_text": "Which is the most prominent spinous process",
        "subject_id": 2
    }
][
    {
        "option_code": "A",
        "option_text": "Tibial condyles",
        "option_is_correct": 0
    },
    {
        "option_code": "B",
        "option_text": "Trochanter of femur",
        "option_is_correct": 1
    },
    {
        "option_code": "C",
        "option_text": "Coracoid process of scapula",
        "option_is_correct": 0
    },
    {
        "option_code": "D",
        "option_text": "head of femur",
        "option_is_correct": 0
    },
    {
        "option_code": "A",
        "option_text": "T1",
        "option_is_correct": 0
    },
    {
        "option_code": "B",
        "option_text": "C7",
        "option_is_correct": 1
    },
    {
        "option_code": "C",
        "option_text": "C6",
        "option_is_correct": 0
    },
    {
        "option_code": "D",
        "option_text": "L5",
        "option_is_correct": 0
    }
]

My approach was to somehow push $options inside $output, but even after several tries, I am not able to find the correct way.

1 Answer 1

1

You are creating two separate arrays with their own data, you need to build the question and add the options under that question...

while ($row = $result->fetch_assoc()) {
    $question = [
        "qid"              => $row["qid"],
        "question_text"    => $row["question_text"],
        "subject_id"       => $row["subject_id"],
        "options"          => []
    ];

    $question['options'][] = [
        "option_code" => "A",
        "option_text" => $row["option_01_text"],
        "option_is_correct" => $row["option_01_is_correct"],
    ];
    // And other options...

    $options[] = $question;
}
echo json_encode($options, JSON_PRETTY_PRINT);

One other point is that you may find normalizing your database tables useful. So you have a question and answer table. This allows you to have as many (or few) answers per question and stops repeating fields in 1 table.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for your prompt reply. Earlier in my different attempts, I was making mistake in understanding the concept of using $question['options'][]. Now I got it
Regarding Normalization of the database table - I read about basic rules of normalization, but I am not able to understand what changes I should make. Can you please point out in short. Thank you once again
Have a look at dba.stackexchange.com/questions/23630/…, you may also need something on the answer table to indicate the correct answer, but the principle starts there.

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.