php查到的内容追加到html,javascript - 请问php中如何将查询出来的结果数组转化成自己想要的格式,并在前台利用js输出到html中...
考試類型的表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來歸類一下再輸出)
目前正在學習中,很多地方不是很懂,求指教~~謝謝
總結
以上是生活随笔為你收集整理的php查到的内容追加到html,javascript - 请问php中如何将查询出来的结果数组转化成自己想要的格式,并在前台利用js输出到html中...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: layui根据name获取对象_layu
- 下一篇: 作文第一次用计算机350,第一次考试作文