Hey guys, quick question, I have a query that will usually return multiple results from a database, while I know how to return one result, I am not sure how to return multiple in jquery. I just want to take each of the returned results and run them through my prepare function. I have been trying to use 'for' to handle the array of data but I don't think it can work since I am returning different array values. If anyone has any suggestions, I would really appreciate it.
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 PROCESSING
$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($qryuserscou开发者_开发知识库nt1);
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;
}
EDIT ONLINE USERS FUNCTION
function online_users(response) {
count2++;
var string = '<div class="update-entry"><li id="online_list-'+count2+'">'
+ ''+response.entry+''
+'</li></div>';
return string;
}
Hopefully this will help you get pointed in the right direction:
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;
?>
Why do you need to return multiple results? For something as simple as that, just format everything on the server-side and return all the HTML at once, then have jQuery just put it in. Don't make the client-side have to process things because it will likely be slower.
精彩评论