如何使用json / jquery返回多个数组项

时间:2010-04-28 15:49:12

标签: php jquery

嘿伙计们,快速提问,我有一个查询通常会从数据库中返回多个结果,而我知道如何返回一个结果,我不知道如何在jquery中返回多个。我只想获取每个返回的结果并通过我的prepare函数运行它们。我一直在尝试使用'for'来处理数据数组,但我不认为它可以工作,因为我返回不同的数组值。如果有人有任何建议,我会非常感激。

JQUERY RETRIEVAL

 success: function(json) {

 for(i=0; i < json.rows; i++) {

                $('#users_online').append(online_users(json[i]));
                $('#online_list-' + count2).fadeIn(1500);
} 

} 

PHP处理

$qryuserscount1="SELECT active_users.username,COUNT(scrusersonline.id) AS rows FROM scrusersonline LEFT JOIN active_users ON scrusersonline.id=active_users.id WHERE topic_id='$topic_id'";
$userscount1=mysql_query($qryuserscount1);
while ($row = mysql_fetch_array($userscount1)) {
$onlineuser= $row['username'];
$rows=$row['rows'];
if ($username==$onlineuser){
    $str2=  "<a href=\"statistics.php?user=$onlineuser\"><div class=\"me\">$onlineuser</div></a>";
    }
else {
$str2= "<b><a href=\"statistics.php?user=$onlineuser\"><div class=\"others\">$onlineuser</div></a></b>";
}
$data['rows']=$rows;
$data['entry']=$str1.$str2;
}

编辑在线使用者功能

function online_users(response) {
  count2++;

  var string = '<div class="update-entry"><li id="online_list-'+count2+'">'
      + ''+response.entry+''
      +'</li></div>';

  return string;
}

2 个答案:

答案 0 :(得分:1)

希望这有助于您指明正确的方向:

foo.php

<html>
  <head>
    <script type="text/javascript" src="user_list.js"></script>
    <style type="text/css" media="screen">
      /* instead of using html tags for markup, use CSS */
      #users_online .me {}
      #users_online .other { font-weight: bold; }
    </style>
  </head>
  <body>
    <div id="content">foo</div>
    <div id="users_online">
      <div class="count"></div>
      <div class="list"></div>
    </div>
  </body>
</html>

user_list.js

<script type="text/javascript" charset="utf-8">
  (function($){

    var refresh_user_list = function(){

      // get the data
      $.getJSON("/user_list.php", function(data)){

        // insert HTML into DOM 
        $("#users_online .count").html(data['count']);
        $("#users_online .list").html(data['users']);

      };

      // refresh users list again every 15 seconds
      setTimeout(refresh_user_list, 15000);
    };

    // after the page is ready, get the user list
    $(document).ready(function(){
      refresh_user_list();
    });
  })(jQuery);
</script>

user_list.php

<?php

// header
header("Content-Type: application/json");

// return data
$data = array(
  "users" => "",
  "count" => 0
);

// query users
$result = mysql_query("
  SELECT
    active_users.username,
    COUNT(scrusersonline.id) AS rows

  FROM scrusersonline

  LEFT JOIN active_users ON scrusersonline.id=active_users.id

  WHERE topic_id='$topic_id';
");

// load users
while($u = mysql_fetch_object($result)){
  $link_class = ($username == $u->username)
    ? "me"
    : "other"
  ;
  // don't use <b> tag here. define bold in the stylesheet with the link's class
  $data["users"]  .= "<a class=\"${link_class}\" href=\"statistics.php?user={$u->username}\">{$u->username}</a>";
}

// load count
// this is sort of silly, but I don't know the way your database is setup so it's hard to advise you how to improve it
$data["count"] = $u->rows;

// return the result
echo json_encode($data);

// make sure you stop the script here so nothing else gets output
exit;
?>

答案 1 :(得分:0)

为什么需要返回多个结果?对于简单的事情,只需在服务器端格式化所有内容并立即返回所有HTML,然后让jQuery将其放入。不要让客户端必须处理事情,因为它可能会更慢。