考試類型的表jx_exam_type,可後台新增內容
考試成績的表格jx_result,可後台加入內容
#期中考試成績表中的exam_id對應考試類型表中的id,也就是新增的成績是屬於期中還是期末
然後使用php查詢
$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);
輸出的格式如下
[
{
"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"
}
]
請問我如何才能將以上輸出的json格式變成以下這種
{
"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"
}
]
}
變成以上這種格式後輸出到前台,透過JS來輸出到html上面
(可能我寫的想要的格式有問題,不過大概意思就是將原來的資料依照exam_id來歸類一下再輸出)
目前正在學習中,很多地方不是很懂,求指教~~謝謝
女神的闺蜜爱上我2017-06-07 09:25:20
這是我理解的架構,這樣前端應該可以遍歷到
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)
滿天的星座2017-06-07 09:25:20
你取資料和處理資料的邏輯有問題,不過你這種的話也可以寫
<?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));
不過你這種取資料的方式不建議,如果資料複雜點 量大點,處理起來就會麻煩