We help IT Professionals succeed at work.

need help in making json object

3xtr3m3d
3xtr3m3d used Ask the Experts™
on
Hi

this is the code i use to generate single json object

$SQL = mysql_query("SELECT * FROM `receipts` WHERE DATE(date) = '2011-08-03'");
	if(mysql_num_rows($SQL ) > 0){
		$i=0;
		$responce->success = true;
		while($SQL_RESULT = mysql_fetch_object($SQL)){
			$responce->data[$i]['reciept_no'] = $SQL_RESULT->reciept_no;
			$responce->data[$i]['time'] = $SQL_RESULT->date;
			$responce->data[$i]['user'] = $SQL_RESULT->user;
			$i++;
		}
	}
	else{
		$responce->success = false;	
		$responce->data = '';
		$responce->reason = "No Activity...";
	}

	echo json_encode($responce);

Open in new window


result

{"success":true,"data":[{"reciept_no":"2411","time":"09:33:56 AM","user":"test"},
{"reciept_no":"2412","time":"11:29:01 AM","user":" test "}]}

Open in new window


so there is another query which similar to this and generate exact same kind of output but from a another mysql table

i want to do is combine two results and send to javascript then decode it in javascript

like wrap first result with like table1 second result with table 2 or something

how to do that?

Sorry for the bad english

Regards

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sounds like you simply need to modify your SQL statement...

SELECT * FROM `table1` t1, `table2` t2 WHERE t1.DATE(date) = '2011-08-03';
More like...

SELECT * FROM `table1` t1, `table2` t2 WHERE t1.DATE(date) = '2011-08-03' and t2.DATE(date) = '2011-08-03' ;

Then add the necessary columns to your $responce variable from table 2.

$responce->data[$i]['reciept_no'] = $SQL_RESULT->reciept_no;
$responce->data[$i]['time'] = $SQL_RESULT->date;
$responce->data[$i]['user'] = $SQL_RESULT->user;

$responce->data[$i]['column1'] = $SQL_RESULT->reciept_no;
$responce->data[$i]['column2'] = $SQL_RESULT->date;
$responce->data[$i]['column3'] = $SQL_RESULT->user;

If you can join the tables that would be a better way to go...

SELECT *
FROM db.table1 AS t1, db.table2 AS t2
WHERE t1.id = t2.id
    AND t1.DATE(date) = '2011-08-03;

Author

Commented:
hi thanks for reply.  not really what i expect. doesnt need to combine query just need to wrap each results so i can get two objects by decoding at javascript end

like

 table1[{"success":true,"data":[{"reciept_no":"2411","time":"09:33:56 AM","user":"test"},
{"reciept_no":"2412","time":"11:29:01 AM","user":" test "}]}]

table2[{"success":true,"data":[{"reciept_no":"2411","time":"09:33:56 AM","user":"test"},
{"reciept_no":"2412","time":"11:29:01 AM","user":" test "}]}]

Open in new window

greetings 3xtr3m3d, I do not see that it would be necessary to have more than one object, you already can place an unlimited number of entries in the-
$responce->data[$i]

you will just need to store the $i number where your second data set starts, and the $i number where the third data set starts, etc.
// below is SECOND SELECT
$SQL2 = mysql_query("SELECT * FROM `receipts2` WHERE DATE(date) = '2011-08-03'");
	if(mysql_num_rows($SQL2 ) > 0){
		//$i=0; // do NOT reset $i
		$responce->success = true;
                $responce->data2start = $i;//record start position
		while($SQL_RESULT = mysql_fetch_object($SQL2)){
			$responce->data[$i]['reciept_no'] = $SQL_RESULT->reciept_no;
			$responce->data[$i]['time'] = $SQL_RESULT->date;
			$responce->data[$i]['user'] = $SQL_RESULT->user;
			$i++;
		}
	}
	else{
		$responce->success2 = false;
		//$responce->data = '';
		$responce->reason2 = "No Activity...";
	}

// OR use a second data set in your $responce
$responce->data2[$i]['reciept_no'] = $SQL_RESULT->reciept_no;

Open in new window

Sandeep KothariProject Lead

Commented:
do json encode only when you have a total array...

say for example...

when you get $response1 from table1 and $response2 from table2 .. .. do something like this...

$final_arr = array("table1" => $response1, "table2" => $response2);

$final_json = json_encode($final_arr);

Author

Commented:
Thanks