亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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
某草草
某草草 2017-06-07 09:23:20
0
3
856

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": "三年級(jí)期末考試",
        "etime": "2017-06-02"
    },
    {
        "type": "英語",
        "score": "89",
        "exam_id": "2",
        "title": "三年級(jí)期末考試",
        "etime": "2017-06-02"
    },
    {
        "type": "數(shù)學(xué)",
        "score": "60",
        "exam_id": "2",
        "title": "三年級(jí)期末考試",
        "etime": "2017-06-02"
    },
    {
        "type": "數(shù)學(xué)",
        "score": "91",
        "exam_id": "1",
        "title": "三年級(jí)期中考試",
        "etime": "2017-05-25"
    },
    {
        "type": "語文",
        "score": "85",
        "exam_id": "1",
        "title": "三年級(jí)期中考試",
        "etime": "2017-05-25"
    },
    {
        "type": "英語",
        "score": "87",
        "exam_id": "1",
        "title": "三年級(jí)期中考試",
        "etime": "2017-05-25"
    }
]

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

{
    "title": "三年級(jí)期中考試",
    "etime": "2017-05-25",
    "exam_id": [
        {
            "type": "數(shù)學(xué)",
            "score": "91",
            "exam_id": "1"
        },
        {
            "type": "語文",
            "score": "85",
            "exam_id": "1"
        },
        {
            "type": "英語",
            "score": "87",
            "exam_id": "1"
        }
    ],
    "title": "三年級(jí)期末考試",
    "etime": "2017-06-02",
    "exam_id": [
        {
            "type": "語文",
            "score": "91",
            "exam_id": "2"
        },
        {
            "type": "英語",
            "score": "89",
            "exam_id": "2"
        },
        {
            "type": "數(shù)學(xué)",
            "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

某草草
某草草

reply all(3)
女神的閨蜜愛上我

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)
阿神

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

滿天的星座

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": "三年級(jí)期末考試",
        "etime": "2017-06-02"
    },
    {
        "type": "英語",
        "score": "89",
        "exam_id": "2",
        "title": "三年級(jí)期末考試",
        "etime": "2017-06-02"
    },
    {
        "type": "數(shù)學(xué)",
        "score": "60",
        "exam_id": "2",
        "title": "三年級(jí)期末考試",
        "etime": "2017-06-02"
    },
    {
        "type": "數(shù)學(xué)",
        "score": "91",
        "exam_id": "1",
        "title": "三年級(jí)期中考試",
        "etime": "2017-05-25"
    },
    {
        "type": "語文",
        "score": "85",
        "exam_id": "1",
        "title": "三年級(jí)期中考試",
        "etime": "2017-05-25"
    },
    {
        "type": "英語",
        "score": "87",
        "exam_id": "1",
        "title": "三年級(jí)期中考試",
        "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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template