ebolt007 Posted February 10, 2012 Share Posted February 10, 2012 I have a function where I am returning a few different arrays into one return json_encode(); but how would I do this? I'm bulling an array from 2 different database tables, and I can't join or union these, because I am actually going to have quite a few different calls that needs to call into other areas. Anyway, My 2 variables are,' $names and $posts If I put one of these in the return json_encode($posts); like that, then the $posts show up fine while the names of course display Undefined, and if I put in return json_encode($names); then my code works for the names to be displayed but anything in the posts of course is undefined, how do I put these two together? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/256788-2-different-array-variables-in-one-json_encode/ Share on other sites More sharing options...
ebolt007 Posted February 10, 2012 Author Share Posted February 10, 2012 OK Update I just tried array_merge so I have something like $json = array_merge($names, $posts); return json_encode($json); which works, BUT, now my display looks like undefinedName1 undefinedName1 undefinedName1 undefinedName2 undefinedName2 Post1undefined Post1undefined Post1undefined Post2undefined Post3undefined So that's not really my fix either, maybe this is a javascript question tho, because I am using jguery to postsJSON and get the variables and display them from the Json, but I'm not too good with Javascript. ugh. Quote Link to comment https://forums.phpfreaks.com/topic/256788-2-different-array-variables-in-one-json_encode/#findComment-1316421 Share on other sites More sharing options...
ebolt007 Posted February 10, 2012 Author Share Posted February 10, 2012 would implode or explode help put these arrays together? I've tried everything I can think of including array_merge(implode($names, $posts)); nothing works and I SUCK with arrays. Anyone need more info? Here is the javascript I am trying to pull this into. $(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('id',id) //generate the HTML .html('<tr><td>' + post.Post + '' + post.Name+'</td></tr>') .click(function() { window.location = postURL; }) //inject into the container .appendTo($('#posts')) .hide() .slideDown(250,function() { if(i == 0) { $.scrollTo($('div#' + id)); } }); }); }; Quote Link to comment https://forums.phpfreaks.com/topic/256788-2-different-array-variables-in-one-json_encode/#findComment-1316823 Share on other sites More sharing options...
kicken Posted February 10, 2012 Share Posted February 10, 2012 Sounds like you need to do one of two things 1) Merge the names into the posts array, putting the names into their associated posts. You can do this with a couple foreach loops probably. 2) Change your JS code to work with two separate arrays instead of of a single array. Then just encode them both, a la json_encode(array($names, $posts)); Quote Link to comment https://forums.phpfreaks.com/topic/256788-2-different-array-variables-in-one-json_encode/#findComment-1316837 Share on other sites More sharing options...
ebolt007 Posted February 10, 2012 Author Share Posted February 10, 2012 Thanks Kicken, but any ideas on how I could do the 2nd one? I'm fairly new to Ajax and Jquery and learning it as I go, I've always been pretty much straight php and have this piece settup exactly the way I want it with straight PHP, but want to integrate AJAX to only load so many Posts (my problem is, those posts then have comments and like features, I can get just the comments to display this way but the important stuff is missing), then a user can click see more to have more then displayed down the page. Quote Link to comment https://forums.phpfreaks.com/topic/256788-2-different-array-variables-in-one-json_encode/#findComment-1316844 Share on other sites More sharing options...
ebolt007 Posted February 10, 2012 Author Share Posted February 10, 2012 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. Below is the results I get, and I want to combine the 2 queries because the information that I get is correct, however they are basically seperate arrays, hence the Undefined. I have something like that to get the arrays, $json = array_merge($names, $posts); return json_encode($json); and this is the result by using array_merge undefinedName1 undefinedName1 undefinedName1 undefinedName2 undefinedName2 Post1undefined Post1undefined Post1undefined Post2undefined Post3undefined Below if the javascript piece that I am using for this, can I split out the .html()? Because I have also done a bunch of unions and LEFT JOINS to my sql and can get most of the information I want into one array, but when I do this and try to echo the Posts, then it will echo the post 3 times instead of once if I have 3 comments, so I can't strip out the comments this way. Or is there a better way of doing this? Below is my javascript. $(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('id',id) //generate the HTML .html('<tr><td>' + post.Name+ '' + post.Post+'</td></tr>') .click(function() { window.location = postURL; }) //inject into the container .appendTo($('#posts')) .hide() .slideDown(250,function() { if(i == 0) { $.scrollTo($('div#' + id)); } }); }); }; My biggest problem is only getting one post, but getting all the comments that were posted to this array doing it this way. What I really want to do is integrate multiple arrays, so I don't have to just use +post.Name+ but I can also use +name.Name+ and this will pull from a different json_encode so I could have json_encode($name); and json_encode(post); and work with them this way. Quote Link to comment https://forums.phpfreaks.com/topic/256788-2-different-array-variables-in-one-json_encode/#findComment-1316847 Share on other sites More sharing options...
ebolt007 Posted February 13, 2012 Author Share Posted February 13, 2012 Anyone know about this? I mean I understand, post.PostID pulls the PostID from the arrays, but where does the above script create post.? so I can create like a name.Username then post.PostID to loop thru both of these? And bigger yet, how do I create a loop in javascript to grab post.PostID then name.Username and display thos, and then inside that I'd have comment.Comments loop thru? Anyone? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/256788-2-different-array-variables-in-one-json_encode/#findComment-1317912 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.