Link to home
Start Free TrialLog in
Avatar of 3xtr3m3d
3xtr3m3d

asked on

need help in making json object

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

Avatar of Giovanni
Giovanni
Flag of United States of America image

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;
Avatar of 3xtr3m3d
3xtr3m3d

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);

Thanks