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

考试类型的表jx_exam_type,可后台添加内容

89634deb55a247b0fcb36e7a6eb557f0.png

考试成绩的表jx_result,可后台添加内容

dcbeec4a21f0f32fe5ed428dd5c8a5dc.png

期中考试成绩表中的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来归类一下再输出)

目前正在学习中,很多地方不是很懂,求指教~~谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值