我试图通过将所有数组编码为 JSON 数组来返回 2 个数组和第三个变量。我想访问数组和第三个变量的所有键。我很难做到这一点。我所能做的就是这个,没有任何成功..
需要帮助。有没有办法可以将整个 json_encoded data['states'] 复制到 JS 数组中?
在线学生.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']);
});
}