2011-08-08 2 views
0

이 난 단일 JSON 객체를 생성하는 데 사용하는 코드이다두 개의 mysql 쿼리에 대해 json을 빌드하는 방법은 무엇입니까?

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

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

그래서이 출력 동일한 종류의 생성과 유사한 다른 쿼리이지만 서로 같은 결과는 내가 원하는 MySQL의 테이블

는 자바 스크립트

사장님 그것을 디코딩이 개 결과를 결합하고 자바 스크립트로 보낼 수 있습니다 첫 번째 결과를 table1과 같은 두 번째 결과 또는 두 번째 테이블로 감싸기

어떻게 수행하나요? 나쁜 영어

감사

답변

0

에 대한

죄송 JSON은 데이터 구조의 단지 텍스트 표현입니다. 단일 구조로 두 개의 분리 된 결과를 저장하려면, 다음

data['response #1'] = 'blah blah blah'; 
data['response #2'] = 'other other other'; 

당신은 하나의 하위 배열에 두 개의 쿼리의 데이터를 저장할 수 없지만, 당신은 할 수있는 몇 가지 추가 데이터가 필요 것 두 데이터 소스를 구분합니다. "이 레코드는 쿼리 # 1 또는 쿼리 # 2에서 나왔습니까?" 당신은 두 쿼리, 하나 개 $ response1에 출력 및 $ response2에 다른 실행할 수

+0

게시 된 데이터는 query1에서 가져 왔지만 쿼리 2는 동일한 종류의 결과를 생성합니다. 무슨 일을하려고하면 하나의 요청으로 두 개의 extj 저장소를로드하는 것입니다. –

2

, 당신은 사용할 수 있습니다

echo json_encode(array('table1'=>$response1,'table2'=>$response2)); 
0
$SQL = mysql_query("SELECT * FROM `receipts` WHERE DATE(date) = '2011-08-03'"); 
if(mysql_num_rows($SQL) > 0){ 
    $i=0; 
    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++; 
    } 
} 

$SQL = mysql_query("SELECT * FROM `receipts2` 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); 

귀하의 질문인가요?

관련 문제