日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php查到的内容追加到html,javascript - 请问php中如何将查询出来的结果数组转化成自己想要的格式,并在前台利用js输出到html中...

發布時間:2025/3/12 php 17 豆豆

考試類型的表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中...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。