search

Home  >  Q&A  >  body text

javascript - How to convert the query result array into the format you want in php, and use js to output it to html in the foreground

Exam type table jx_exam_type, content can be added in the background

Examination score table jx_result, content can be added in the background

The exam_id in the midterm exam score table corresponds to the id in the exam type table, that is, whether the added score belongs to midterm or final

Then use php query

$sql="SELECT re.type, re.score, re.exam_id, et.title, DATE_FORMAT(et.addtime, '%Y-%m-%d') AS etime FROM jx_result AS re LEFT JOIN jx_exam_type AS et ON re.exam_id = et.id WHERE re.uid = '$uid' ORDER BY et.addtime DESC";

$result=$db->query($sql);

while($row=$result->fetch_assoc()){
    $arr[]=$row;
}

echo json_encode($arr);

The output format is as follows

[
    {
        "type": "语文",
        "score": "91",
        "exam_id": "2",
        "title": "三年级期末考试",
        "etime": "2017-06-02"
    },
    {
        "type": "英语",
        "score": "89",
        "exam_id": "2",
        "title": "三年级期末考试",
        "etime": "2017-06-02"
    },
    {
        "type": "数学",
        "score": "60",
        "exam_id": "2",
        "title": "三年级期末考试",
        "etime": "2017-06-02"
    },
    {
        "type": "数学",
        "score": "91",
        "exam_id": "1",
        "title": "三年级期中考试",
        "etime": "2017-05-25"
    },
    {
        "type": "语文",
        "score": "85",
        "exam_id": "1",
        "title": "三年级期中考试",
        "etime": "2017-05-25"
    },
    {
        "type": "英语",
        "score": "87",
        "exam_id": "1",
        "title": "三年级期中考试",
        "etime": "2017-05-25"
    }
]

How can I change the above output json format into the following one

{
    "title": "三年级期中考试",
    "etime": "2017-05-25",
    "exam_id": [
        {
            "type": "数学",
            "score": "91",
            "exam_id": "1"
        },
        {
            "type": "语文",
            "score": "85",
            "exam_id": "1"
        },
        {
            "type": "英语",
            "score": "87",
            "exam_id": "1"
        }
    ],
    "title": "三年级期末考试",
    "etime": "2017-06-02",
    "exam_id": [
        {
            "type": "语文",
            "score": "91",
            "exam_id": "2"
        },
        {
            "type": "英语",
            "score": "89",
            "exam_id": "2"
        },
        {
            "type": "数学",
            "score": "60",
            "exam_id": "2"
        }
    ]
}

After changing to the above format, it is output to the front desk and output to the html through JS
(Maybe there is something wrong with the format I want to write, but the general meaning is to classify the original data according to exam_id Then output)

I am currently studying, and I don’t understand many things very well. Please give me some advice~~Thank you

某草草某草草2736 days ago761

reply all(3)I'll reply

  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-07 09:25:20

    This is the architecture I understand, so the front end should be able to traverse it

    while ($row = $result->fetch_assoc()) {
        $scorearr = array("type" => $row["type"], "score" => $row["type"], "exam_id" => $row["exam_id"]);
        if (isset($arr[$row["exam_id"]])) {
            $arr[$row["exam_id"]]["exam_id"][] = $scorearr;
        } else {
            $arr[$row["exam_id"]]["title"] = $row["title"];
            $arr[$row["exam_id"]]["etime"] = $row["etime"];
            $arr[$row["exam_id"]]["exam_id"][0] = $scorearr;
        }
    }
    
    $result = array();
    foreach($arr as $val) {
        $result[] = $val;
    }
    echo $json_encode($result)

    reply
    0
  • 阿神

    阿神2017-06-07 09:25:20

    It should be one less layer of packaging. You can traverse your array, and then you can get what you want, and then transcode it in json

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-07 09:25:20

    There is something wrong with your logic in getting and processing data, but you can still write in this way

    <?php
    $str = '[
        {
            "type": "语文",
            "score": "91",
            "exam_id": "2",
            "title": "三年级期末考试",
            "etime": "2017-06-02"
        },
        {
            "type": "英语",
            "score": "89",
            "exam_id": "2",
            "title": "三年级期末考试",
            "etime": "2017-06-02"
        },
        {
            "type": "数学",
            "score": "60",
            "exam_id": "2",
            "title": "三年级期末考试",
            "etime": "2017-06-02"
        },
        {
            "type": "数学",
            "score": "91",
            "exam_id": "1",
            "title": "三年级期中考试",
            "etime": "2017-05-25"
        },
        {
            "type": "语文",
            "score": "85",
            "exam_id": "1",
            "title": "三年级期中考试",
            "etime": "2017-05-25"
        },
        {
            "type": "英语",
            "score": "87",
            "exam_id": "1",
            "title": "三年级期中考试",
            "etime": "2017-05-25"
        }
    ]';
    
    $arr = json_decode($str,true);
    
    $result = [];
    $examId = [];
    foreach($arr as $key=>$val){
    
        if(!isset($result[$val['exam_id']])){
            $result[$val['exam_id']]['title'] = $val['title'];
            $result[$val['exam_id']]['etime'] = $val['etime'];
        }
    
        $result[$val['exam_id']]['exam_id'][] = array("type"=>$val['type'],"score"=>$val['score'],"exam_id"=>$val['exam_id']);
    
    }
    
    
    echo json_encode(array_values($result));

    However, your method of obtaining data is not recommended. If the data is more complex and larger, it will be troublesome to process.

    reply
    0
  • Cancelreply