IC SunsetThe developerWorks Connections platform will be sunset on December 31, 2019. On January 1, 2020, this forum will no longer be available. More details available on our FAQ.
  • No replies
3 Posts

Pinned topic 2 Ajax responses from json_encode

‏2012-02-16T16:50:55Z |
So I have a script I'm working on that pulls multiple comments from a database, then puts it in a jquery piece that pulls the data from the arrays I created in my MYSQL Select.

function get_posts($start = 0, $number_of_posts = 5) 
{ $posts = array(); $comments = array(); $query = 
" SELECT PostID FROM Users_WallPosts WHERE Users_WallPosts.UserID = 
'$user_ID'   ORDER BY DateAdded DESC LIMIT $start,$number_of_posts
"; $result = mysql_query($query); 

while($row = mysql_fetch_assoc($result)) 
{ $posts[] = $row;   $postsmain = $row[
'PostID']; $sql2 = 
" SELECT Users_WallComments.Comment FROM Users_WallComments LEFT JOIN Users_WallPosts ON Users_WallPosts.PostID = Users_WallComments.PostID where Users_WallPosts.PostID = 
"; $result2 = mysql_query($sql2); 

while($comments_row = mysql_fetch_assoc($result2)) 
{ $comments[] = $comments_row; 
} json_encode($comments); 

return json_encode($posts); 

Now I want it to pull those comments and posts in, I can get it to pull in one or the other, but I can't get the Posts to pull in, then the comments to pull in below that, basically like facebook. But I have this settup to only display so many comments then when you click the show more it loads more below the 5 so it doesn't load everything at the same time. I simplified my sql above, but the SQL's work fine, I'm good with PHP and sql, it's AJAX that I'm new too and confused with he below code. I'm guessing I need to json_encodes, and then use those, but how do I break these apart and use 2 json_encodes for my arrays above?

<script type=
//when the DOM is ready $(document).ready(function()
//settings on top var domain = 
'http://test.com/'; var initialPosts = <?php echo get_posts(0,$_SESSION[
'posts_start']); ?>; 
//function that creates posts var postHandler = function(postsJSON) 
{ $.each(postsJSON,function(i,post) 
//post url var postURL = 
'' + domain + post.PostID; var id = 
'post-' + post.PostID; 
//create the HTML $(
'<div></div>') .addClass(
'post')   .attr(
//generate the HTML .html(
'<tr><td>' + post.Post + 
''*then I want my comments to loop thru here like comments.Comment*
'</td></tr>')   .click(function() 
{ window.location = postURL; 
//inject into the container .appendTo($(
'#posts')) .hide() .slideDown(250,function() 

if(i == 0) 
{ $.scrollTo($(
'div#' + id)); 
//place the initial posts in the page postHandler(initialPosts); 
//first, take care of the "load more" 
//when someone clicks on the "load more" DIV var start = <?php echo $_SESSION[
'posts_start']; ?>; var desiredPosts = <?php echo $number_of_posts; ?>; var loadMore = $(
//load event / ajax loadMore.click(function()
//add the activate class and change the message loadMore.addClass(
//begin the ajax attempt $.ajax(
{ url: 
'indextest.php', data: 
'start': start, 
'desiredPosts': desiredPosts 
}, type: 
'get', dataType: 
'json', cache: false, success: function(responseJSON) 
//reset the message loadMore.text(
'Load More'); 
//increment the current status start += desiredPosts; 
//add in the new posts postHandler(responseJSON); 
//failure class error: function() 
//reset the message loadMore.text(
'Oops! Try Again.'); 
//complete event complete: function() 
//remove the spinner loadMore.removeClass(
}); </script>