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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置