ibrahimjan Posted May 3, 2012 Share Posted May 3, 2012 (edited) Please have a look at my json data to see what I want to do: [ { "weID": "-126555895", "size_h": "700", "size_w": "494", "images": [ { "imgID": "1535689403", "imgName": "1321865407" } ], "text": [ { "txtID": "-1892792786", "txtText": "4.99" }, { "txtID": "1583276033", "txtText": "$" } ] }, // i cant seem to loop through my data to get more results looking like this:{ "weID": "3444555435", "size_h": "500", "size_w": "294", "images": [ { "imgID": "-454646555", "imgName": "77774433" }, { "imgID": "1535689403", "imgName": "1321865407" } ], "text": [ { "txtID": "15834443333", "txtText": "more text" } ] }] //my PHP code: $back = mysql_query("SELECT weID, size_h, size_w FROM wb WHERE weID=-126555895");$images = mysql_query("SELECT imgID, imgName FROM wi WHERE weID=-126555895");$text= mysql_query("SELECT txtID, txtText FROM wt WHERE weID=-126555895"); //all my above SQL statements have weID in common, how can I link them together, to show all my results rather then one "-126555895"?? $model = array(); while($e = mysql_fetch_assoc($back)){ $model['weID'] = $e['weID']; $model['size_h'] = $e['size_h']; $model['size_w'] = $e['size_w']; while($h = mysql_fetch_assoc($images)) { $model['images'][] = array( 'imgID' => $h['imgID'], 'imgName' => $h['imgName'] ); while($f = mysql_fetch_assoc($text)){ $model['text'][] = array( 'txtID' => $f['txtID'], 'txtText' => $f['txtText'] );}; };}; echo json_encode ($model); mysql_close($con); I need it to loop through so it can display all the records in Edited May 3, 2012 by Ibrahim Home Jan Link to comment Share on other sites More sharing options...
ckrudelux Posted May 3, 2012 Share Posted May 3, 2012 Please have a look at my json data to see what I want to do: [ { "weID": "-126555895", "size_h": "700", "size_w": "494", "images": [ { "imgID": "1535689403", "imgName": "1321865407" } ], "text": [ { "txtID": "-1892792786", "txtText": "4.99" }, { "txtID": "1583276033", "txtText": "$" } ] }, // i cant seem to loop through my data to get more results looking like this:{ "weID": "3444555435", "size_h": "500", "size_w": "294", "images": [ { "imgID": "-454646555", "imgName": "77774433" }, { "imgID": "1535689403", "imgName": "1321865407" } ], "text": [ { "txtID": "15834443333", "txtText": "more text" } ] }] //my PHP code: $back = mysql_query("SELECT weID, size_h, size_w FROM wb WHERE weID=-126555895");$images = mysql_query("SELECT imgID, imgName FROM wi WHERE weID=-126555895");$text= mysql_query("SELECT txtID, txtText FROM wt WHERE weID=-126555895"); //all my above SQL statements have weID in common, how can I link them together, to show all my results rather then one "-126555895"?? $model = array(); while($e = mysql_fetch_assoc($back)){ $model['weID'] = $e['weID']; $model['size_h'] = $e['size_h']; $model['size_w'] = $e['size_w']; while($h = mysql_fetch_assoc($images)) { $model['images'][] = array( 'imgID' => $h['imgID'], 'imgName' => $h['imgName'] ); while($f = mysql_fetch_assoc($text)){ $model['text'][] = array( 'txtID' => $f['txtID'], 'txtText' => $f['txtText'] );}; };}; echo json_encode ($model); mysql_close($con); I need it to loop through so it can display all the records in From what I can tell you have nested your loops this will result in an error. They need to be separated for it to work. Link to comment Share on other sites More sharing options...
thescientist Posted May 3, 2012 Share Posted May 3, 2012 there's nothing wrong with nesting loops. If you want to make multidimensional array's like the OP wants to then, that is one way to to accomplish it. to the OP, I can't see what is wrong with your two cases. Are you saying multiple images are not getting pushed correctly to the images array? From what I can tell it looks fine. Are you sure $back, $images, and $text are what you expect them to be? Link to comment Share on other sites More sharing options...
ibrahimjan Posted May 4, 2012 Author Share Posted May 4, 2012 Ok, what I need to do is loop through $back to show all rows, at the moment its only displaying one which is "-126555895" obviously as set in the sql statement, but maybe I need to change my sql to show all records of $back, but $images and $text need all to show corresponding joint data by "weID" and I am having proper difficulties joining them. At the moment it's easy to set up because we are only showing one record "-126555895", I need to show all the records in the above json layout. I hope this makes senesce, its very hard to explain multidimensional arrays, any help is appreciated. Link to comment Share on other sites More sharing options...
birbal Posted May 4, 2012 Share Posted May 4, 2012 http://www.google.co.in/url?sa=t&rct=j&q=join+using+mysql&source=web&cd=1&ved=0CCoQFjAA&url=http%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.0%2Fen%2Fjoin.html&ei=IT-jT-H_AcmsrAfJxrTbDQ&usg=AFQjCNGXZQycUZ9UHnvO4i2-l6UslF5wRA you can use join in query which will be more efficient Link to comment Share on other sites More sharing options...
ibrahimjan Posted May 5, 2012 Author Share Posted May 5, 2012 Ok I think I got my SQL sorted, it's a full array now, but have to change my original PHP.Any help in changing my original PHP to outpu like the above json string, is much appreciated. "SELECT wb.weID, size_h, size_w, imgID, imgName, txtID, txtText FROM wb JOIN wi ON wi.weID = wb.weID JOIN wt ON wt.weID = wb.weID WHERE wb.weID = '-126555895'" Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now