hackalive Posted June 4, 2012 Share Posted June 4, 2012 Hi Guys, Currently om piecing together my JSON in a rather crap way (for want of a better word) $id = "1"; // CALLED FROM USERS TABLE $first_name = "John"; // CALLED FROM USERS TABLE $last_name = "Smith"; // CALLED FROM USERS TABLE $name = $first_name.' '.$last_name; $username = "jsmith"; // CALLED FROM USERS TABLE $birthday = "01/01/2001"; // CALLED FROM USERS TABLE $gender = "male"; // CALLED FROM USERS TABLE $hometownid = "10"; // CALLED FROM USERS TABLE $hometown = "New York"; // FROM ANOTHER TABLE THAT REFS THE ID $locationid = "11"; // CALLED FROM USERS TABLE $location = "London"; // FROM ANOTHER TABLE THAT REFS THE ID $output1 = array('id'=>$id,'name'=>$name,'first_name'=>$first_name,'last_name'=>$last_name,'usernmae'=>$username,'birthday'=>$birthday,'gender'=>$gender,'hometown'=>array('id'=>$hometownid,'name'=>$hometown),'location'=>array('id'=>$locationid,'name'=>$location)); $output = json_encode($output1); How could I create the $output1 in a cleaner and neater way? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/263615-a-cleaner-way-to-achieve-json/ Share on other sites More sharing options...
devWhiz Posted June 4, 2012 Share Posted June 4, 2012 $output1['id'] = $id; $output1['name'] = $name; $output1['first_name'] = $first_name; $output1['last_name'] = $last_name; $output1['username'] = $username; $output1['birthday'] = $birthday; $output1['gender'] = $gender; $output1['hometown']['id'] = $hometownid; $output1['hometown']['name'] = $hometown; $output1['location']['id'] = $locationid; $output1['location']['name'] = $location; Quote Link to comment https://forums.phpfreaks.com/topic/263615-a-cleaner-way-to-achieve-json/#findComment-1351016 Share on other sites More sharing options...
hackalive Posted June 4, 2012 Author Share Posted June 4, 2012 I know doing $result = $mysqli->query($query); $array = $result->fetch_assoc(); json_encode($array) is the nicest way to do it if data only comes from one table - but how to do it from multiple sources. Quote Link to comment https://forums.phpfreaks.com/topic/263615-a-cleaner-way-to-achieve-json/#findComment-1351017 Share on other sites More sharing options...
PFMaBiSmAd Posted June 4, 2012 Share Posted June 4, 2012 Your goal would be for your query to return an array with the keys and values that you need. Then simply json_encode that array. but how to do it from multiple sources You should be using one joined query to get all the related data in one query. Without more specific information from you, not possible to directly help. Quote Link to comment https://forums.phpfreaks.com/topic/263615-a-cleaner-way-to-achieve-json/#findComment-1351019 Share on other sites More sharing options...
hackalive Posted June 4, 2012 Author Share Posted June 4, 2012 At the moment this is my total code: $queryA = "SELECT * FROM `user` WHERE `id`='3'"; $resultA = $mysqli->query($queryA); $arrayA = $resultA->fetch_assoc(); if($arrayA['id']) { $id = $arrayA['id']; // CALLED FROM USERS TABLE $first_name = $arrayA['first_name']; // CALLED FROM USERS TABLE $last_name = $arrayA['last_name']; // CALLED FROM USERS TABLE $name = $first_name.' '.$last_name; $username = $arrayA['username']; // CALLED FROM USERS TABLE $birthday = "01/01/2001"; // CALLED FROM USERS TABLE $gender = $arrayA['gender']; // CALLED FROM USERS TABLE $hometownid = $arrayA['hometown']; // CALLED FROM USERS TABLE $queryB = "SELECT * FROM `locations` WHERE `id`='$hometownid'"; $resultB = $mysqli->query($queryB); $arrayB = $resultB->fetch_assoc(); $hometown = $arrayB['name']; // FROM ANOTHER TABLE THAT REFS THE ID $locationid = $arrayA['location']; // CALLED FROM USERS TABLE $queryC = "SELECT * FROM `locations` WHERE `id`='$locationid'"; $resultC = $mysqli->query($queryC); $arrayC = $resultC->fetch_assoc(); $hometown = $arrayC['name']; // FROM ANOTHER TABLE THAT REFS THE ID $output = array('id'=>$id,'name'=>$name,'first_name'=>$first_name,'last_name'=>$last_name,'usernmae'=>$username,'birthday'=>$birthday,'gender'=>$gender,'hometown'=>array('id'=>$hometownid,'name'=>$hometown),'location'=>array('id'=>$locationid,'name'=>$location)); $output = json_encode($output); echo $output; } Hopefully this is of some help to you PFMaBiSmAd (PS: I do need the location & hometown etc to format as those "sub" arrays). Quote Link to comment https://forums.phpfreaks.com/topic/263615-a-cleaner-way-to-achieve-json/#findComment-1351021 Share on other sites More sharing options...
hackalive Posted June 17, 2012 Author Share Posted June 17, 2012 BUMP Quote Link to comment https://forums.phpfreaks.com/topic/263615-a-cleaner-way-to-achieve-json/#findComment-1354552 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.