使用php mysql创建嵌套的json对象 [英] Create nested json object using php mysql

查看:86
本文介绍了使用php mysql创建嵌套的json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,表1有2个字段(question_pk,question_name),表2有4个字段(ans_pk,options,question_fk和right_answer). 我想创建类似以下结构的json

I have two tables, table 1 has 2 fields (question_pk, question_name) and table 2 has 4 fields(ans_pk, options, question_fk and right_answer). I want to create json like the following structure

{
    "type": "quiz",
    "name": "Brand Colors",
    "description": "Can you identify these brands by the background color?",
    "questions": [
        {
            "name": "Can you identify this color?",
            "description": "#ea4c89",
            "answers": [
                {
                    "name": "Dribbble",
                    "description": "dribbble.png",
                    "weight": 1
                },
                {
                    "name": "Amazon",
                    "description": "amazon.png",
                    "weight": 0
                },
                {
                    "name": "Apple",
                    "description": "apple.png",
                    "weight": 0
                }
            ]
        },
        {
            "name": "Can you identify this color?",
            "description": "#3d9ae8",
            "answers": [
                {
                    "name": "Youtube",
                    "description": "youtube.png",
                    "weight": 0
                },
                {
                    "name": "Dropbox",
                    "description": "dropbox.png",
                    "weight": 1
                },
                {
                    "name": "Wordpress",
                    "description": "wordpress.png",
                    "weight": 0
                }
            ]
        },
        {
            "name": "Can you identify this color?",
            "description": "#c4302b",
            "answers": [
                {
                    "name": "Youtube",
                    "description": "youtube.png",
                    "weight": 1
                },
                {
                    "name": "Twitter",
                    "description": "twitter.png",
                    "weight": 0
                },
                {
                    "name": "Vimeo",
                    "description": "vimeo.png",
                    "weight": 0
                }
            ]
        }

    ]
}

我的PHP代码

<?php
include '../config/config.php';
if(isset($_GET['sub_cat_id']))
{
         $sub_cat_id = $_GET['sub_cat_id']; 
        $result = mysql_query("select * from $questions where sub_cat='$sub_cat_id' order by level_fk asc"); 
        $json_response = array(); //Create an array
        $i=1;
                        while ($row = mysql_fetch_array($result))
                        {
                        $row_array['qus_pk'] = $row['qus_pk'];        
                        $row_array['question'] = $row['question'];
                        $qus_pk = $row['qus_pk'];  


                        $option_qry = mysql_query("select * from $qus_ans where qus_pk=$qus_pk");
                        while ($opt_fet = mysql_fetch_array($option_qry))
                        {

                        $row_array['options'] = $opt_fet['options'];  
                        $row_array['right_ans'] = $opt_fet['right_ans'];  
                        array_push($json_response,$row_array); //push the values in the array
                            }                       


                        $i++;
                        }
        echo json_encode($json_response);
}

?>

我的结果是,我得到如下的json响应

And My Result I am getting json response like the following

[
    {
        "qus_pk": "1",
        "question": "Ten years ago, P was half of Q in age. If the ratio of their present ages is 3:4, what will be the total of their present ages?",
        "options": "45",
        "right_ans": "0"
    },
    {
        "qus_pk": "1",
        "question": "Ten years ago, P was half of Q in age. If the ratio of their present ages is 3:4, what will be the total of their present ages?",
        "options": "40",
        "right_ans": "0"
    },
    {
        "qus_pk": "1",
        "question": "Ten years ago, P was half of Q in age. If the ratio of their present ages is 3:4, what will be the total of their present ages?",
        "options": "35",
        "right_ans": "1"
    },
    {
        "qus_pk": "1",
        "question": "Ten years ago, P was half of Q in age. If the ratio of their present ages is 3:4, what will be the total of their present ages?",
        "options": "50",
        "right_ans": "0"
    },
    {
        "qus_pk": "2",
        "question": "Father is aged three times more than his son Sunil. After 8 years, he would be two and a half times of Sunil's age. After further 8 years, how many times would he be of Sunil's age?",
        "options": "4 times",
        "right_ans": "0"
    },
    {
        "qus_pk": "2",
        "question": "Father is aged three times more than his son Sunil. After 8 years, he would be two and a half times of Sunil's age. After further 8 years, how many times would he be of Sunil's age?",
        "options": "1 times",
        "right_ans": "0"
    },
    {
        "qus_pk": "2",
        "question": "Father is aged three times more than his son Sunil. After 8 years, he would be two and a half times of Sunil's age. After further 8 years, how many times would he be of Sunil's age?",
        "options": "3 times",
        "right_ans": "1"
    },
    {
        "qus_pk": "2",
        "question": "Father is aged three times more than his son Sunil. After 8 years, he would be two and a half times of Sunil's age. After further 8 years, how many times would he be of Sunil's age?",
        "options": "5 times",
        "right_ans": "0"
    }
]

每次重新回答问题时,我都会按照自己的意愿进行处理,以及如何避免并如果我想实现第一个json结构,在我的PHP代码中我需要在什么地方进行更改?如果有人知道,请帮助我.

In my respose each time the question is repeated so how to avoid and if i want to achive the first json structure, in my PHP code what&where i need to make changes?. If any one knows help me.

推荐答案

尝试一下,

<?php
include '../config/config.php';
if(isset($_GET['sub_cat_id']))
{
    $sub_cat_id = $_GET['sub_cat_id']; 
    $result = mysql_query("SELECT * FROM $questions WHERE sub_cat='$sub_cat_id' ORDER BY level_fk ASC"); 
    $json_response = array(); //Create an array
    while ($row = mysql_fetch_array($result))
    {
        $row_array = array();
        $row_array['qus_pk'] = $row['qus_pk'];        
        $row_array['question'] = $row['question'];
        $row_array['answers'] = array();
        $qus_pk = $row['qus_pk'];  

        $option_qry = mysql_query("SELECT * FROM $qus_ans WHERE qus_pk=$qus_pk");
        while ($opt_fet = mysql_fetch_array($option_qry))
        {
            $row_array['answers'][] = array(
                'options' => $opt_fet['options'],
                'right_ans' => $opt_fet['right_ans'],
            );

        }
        array_push($json_response, $row_array); //push the values in the array
    }
    echo json_encode($json_response);
}
?>    

这篇关于使用php mysql创建嵌套的json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆