jquery - PHP array to javascript conversion altering the index values -
i have javascript function gets user info database via ajax. has following code.
var temp_id = new object; function checkrequests() { $.ajax({ url: "bin/inc/classes/mechanism_class.php", type: "post", data: {'checkrequest': '1'}, success: function(data1) { for(var i=0; i<jquery.parsejson(data1).length; i++) { $.ajax({ url:"bin/inc/classes/mechanism_class.php", type: "post", data: {'checkrequest2': jquery.parsejson(data1)[i]}, success: function(data) { requestpopper(data); } }).error(function() { }); } } }).error(function() { }); } function requestpopper(data) { var id = jquery.parsejson(data)[0]; var firstname = jquery.parsejson(data)[1]; var lastname = jquery.parsejson(data)[2]; var imagedir = jquery.parsejson(data)[3]; var imageext = jquery.parsejson(data)[4]; var imgsrc = 'uploads/'+imagedir+'/'+'thumbs/'+imagedir+'size2.'+imageext; if($('#'+'requests_content_'+id).length == 0) { if($('.requests_content').length == 0) { $('#requests').after('<div id="dropdown_1"><div class="requests_content" id="requests_content_'+id+'"><a href='+'profile.php?user='+id+'><img src='+imgsrc+' width="60" height="60" class="senders_image"></a><a id="senders_name" href='+'profile.php?user='+id+'>'+firstname + ' ' +lastname+'</a><div id="acceptbutton">accept</div><div id="rejectbutton">reject</div></div></div>'); temp_id.id = id; } else { $('#'+'requests_content_'+temp_id.id).after('<div class="requests_content" id="requests_content_'+id+'"><a href='+'profile.php?user='+id+'><img src='+imgsrc+' width="60" height="60" class="senders_image"></a><a id="senders_name" href='+'profile.php?user='+id+'>'+firstname + ' ' +lastname+'</a><div id="acceptbutton">accept</div><div id="rejectbutton">reject</div></div>'); } } }
also, php class handles ajax requests has following code
class requestsandalerts { public function hasrequests() { if(isset($_post['checkrequest'])) { $current_user = $_session['cred_regiden']; $query1 = "select * `requests` `reqreceiver` = '$current_user' order `sentdatetime` desc"; $senders = array(); if($query_run1 = mysql_query($query1)) { while($res = mysql_fetch_assoc($query_run1)) { $sender = $res['reqsender']; array_push($senders, $sender); } } echo json_encode($senders); } } public function sendrequestinfo() { if(isset($_post['checkrequest2'])) { $sender = $_post['checkrequest2']; $current_user = $_session['cred_regiden']; $info_request_senders = array(); $query1 = "select * `user_credentials` `cred_regiden` = '$sender'"; $query2 = "select * `prof_image` `cred_regiden` = '$sender'"; if($query_run1 = mysql_query($query1)) { while($res = mysql_fetch_assoc($query_run1)) { $info1 = $res['cred_regiden']; $info2 = $res['cred_fname']; $info3 = $res['cred_lname']; array_push($info_request_senders, $info1); array_push($info_request_senders, $info2); array_push($info_request_senders, $info3); } } if($query_run2 = mysql_query($query2)) { while($res2 = mysql_fetch_assoc($query_run2)) { $info4 = $res2['image_dir']; $info5 = $res2['image_extension']; array_push($info_request_senders, $info4); array_push($info_request_senders, $info5); } } echo json_encode($info_request_senders); } } $raa = new requestsandalerts; $raa->hasrequests(); $raa->sendrequestinfo();
now, want extract data of people has sent current user friend request. testing purpose sent user1 2 friend requests user2 , user3 , log in user1 account, when press requests button, on first click user1's info div @ top , user2's info div @ bottom.. here.. when click again, swap places.. in irregular pattern. don't want happen. want user1 on top , user2 on bottom of him according request sent first. did in mysql.. arranged requests in desc order in mysql.. should have arranged div "user1 on top" , "user2 on bottom".. happens randomly.
i guess due fact json_encode randomizes indexes of array.. i'm not sure.. me guys... point out i'm wrong.
the problem multiple ajax call.
note ajax asynchronous http request
example have user1 , user2, in loop first ajax call initialized getting user1 details, hence ajax asynchronous not wait requst. tt send next ajax request getting user2 results. if request 2 completes before req1 .then above error.
solution:
1. user details in first ajax call itself.
or
2 .make second ajax call synchronous.
i choose first option.
Comments
Post a Comment