php - how to return multiple array items using json/jquery -
hey guys, quick question, have query return multiple results database, while know how return 1 result, not sure how return multiple in jquery. want take each of returned results , run them through prepare function. have been trying use 'for' handle array of data don't think can work since returning different array values. if has suggestions, appreciate it.
jquery retrieval
success: function(json) { for(i=0; < 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) rows scrusersonline left join active_users on scrusersonline.id=active_users.id 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; }
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 pointed in 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 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(){ // data $.getjson("/user_list.php", function(data)){ // insert html 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 page ready, 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) rows scrusersonline left join active_users on scrusersonline.id=active_users.id 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 stylesheet link's class $data["users"] .= "<a class=\"${link_class}\" href=\"statistics.php?user={$u->username}\">{$u->username}</a>"; } // load count // sort of silly, don't know way database setup it's hard advise how improve $data["count"] = $u->rows; // return result echo json_encode($data); // make sure stop script here nothing else gets output exit; ?>
Comments
Post a Comment