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
This is the architecture I understand, so the front end should be able to traverse it
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
However, your method of obtaining data is not recommended. If the data is more complex and larger, it will be troublesome to process.