Jump to content

A cleaner way to achieve JSON


hackalive

Recommended Posts

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

Link to comment
Share on other sites

	
$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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.