douzheng0702 2013-08-31 20:11
浏览 107

在PHP中使用JSON返回多个数组

I am trying to return 2 arrays along with a third variable by encoding all of them into a JSON array. I want access to all the keys of both the arrays and also the third variable. I have had a hard time trying to do so. All i was able to do was this, without any success..

Help required. Is there a way I can copy the whole json_encoded data['states'] into a JS array?

onlineStudents.php

<?php
require_once 'myfunctions.php';
$query="Select * from online_students";
$result=  queryMysql($query);
$data["total"]=mysql_num_rows($result)-1;
$query="Select distinct state from online_students";
$result=queryMysql($query);
while($row=  mysql_fetch_array($result))
{
    $query2="select * from online_students where state='$row[state]'";
    $result2=queryMysql($query2);
    $data["states"]=mysql_num_rows($result2);
}
$query="Select distinct college from online_students";
$result=  queryMysql($query);
while($row=  mysql_fetch_array($result))
{
    $query2="Select * from online_students where college='$row[college]'";
    $result2=queryMysql($query2);
    $data["colleges"]=mysql_num_rows($result2);
}
echo json_encode($data);
?>

myfunctions.php

function queryMysql($query)
{
    $result=mysql_query($query) or die(mysql_error());
    return $result;
}

myfunctions.js

function peoples()
{
 $.getJSON("onlineStudents.php",function(data){
        $("#chat_head_number").html(": "+data['total']);
        $("#chat_states_number").html(data['states']);  //i want the whole array data[states] instead of a single value
        $("#chat_college_number").html(data['colleges']);
        });
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 有人会搭建GPT-J-6B框架吗?有偿
    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名