Avatar of 3xtr3m3d
3xtr3m3d

asked on 

generate json data with mysql help

Hi

This is the sample of data type i need to generate with 2 mysql queries

$obj = array(
				"attr" => array(
								"id" => 1,
								"rel" => "drive"					
							 ),
				"data" => "My Documents",
	
		"children" => array(
		array(
				"attr" => array(
								"id" => 2,
								"rel" => "file"
							),
				"data" => "file1.doc",
				"state" => ""
			 )
	)	
);
		
echo json_encode($obj); 

Open in new window


this block should generate with

				"attr" => array(
								"id" => 1,
								"rel" => "drive"					
							 ),
				"data" => "My Documents",

Open in new window


SELECT id,rel,data FROM tbl_drives ORDER BY id

Open in new window


below block should generate with

		"children" => array(
		array(
				"attr" => array(
								"id" => 2,
								"rel" => "file"
							),
				"data" => "file1.doc",
				"state" => ""
			 )

Open in new window


SELECT id,rel,data FROM tbl_files WHERE parent_id = '$id' ORDER BY id

Open in new window



Any idea how to loop two queries to generate above json data?

Thanks
PHPMySQL ServerAJAX

Avatar of undefined
Last Comment
CvD
ASKER CERTIFIED SOLUTION
Avatar of CvD
CvD
Flag of Netherlands image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of 3xtr3m3d
3xtr3m3d

ASKER

Thanks for the reply thats really helpful

problem is only loop

		array(
				"attr" => array(
								"id" => 2,
								"rel" => "file"
							),
				"data" => "file1.doc",
				"state" => ""
			 )

Open in new window


SOLUTION
Avatar of CvD
CvD
Flag of Netherlands image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of 3xtr3m3d
3xtr3m3d

ASKER

still couldn't find a way to loop this array

		array(
				"attr" => array(
								"id" => 2,
								"rel" => "file"
							),
				"data" => "file1.doc",
				"state" => ""
			 )

Open in new window


seems like putting above inside a while loop is impossible
Avatar of CvD
CvD
Flag of Netherlands image

Try someting like below:

function loopArray($arr) {
  foreach ($arr as $key => $val) {
    if (is_array($val) loopArray($val);
    else {
      //do something
    }
  }
}

USAGE:
$obj = array("attr" => array( "id" => 2,"rel" => "file"),"data" => "file1.doc","state" => "");
loopArray($obj);
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo