2014-09-24 85 views
0

我試圖搜索過程輸入與PHP和發送到JSONPHP查詢多個陣列JSON

JSON的輸出是這樣的:

{ 
    "err": 0, 
    "msg": "", 
    "data": { 
     "f": 0, 
     "hotel": [ 
      { 
       "att": 25147, 
       "name": "Crowne Plaza Changi Airport", 
       "city": "Singapore", 
       "country": "Singapore" 
      } 
     ], 
     "city": [ 
      { 
       "att": "-2679652", 
       "name": "Singapore", 
       "region": "", 
       "country": "Singapore", 
       "nr_hotels": "" 
      } 
     ] 
    } 
} 

我與測試序列嘗試,但不起作用

<? 
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")'; 
$result=mysql_query($query);  

// Do Search 
$json_array = array(); 
while ($search=mysql_fetch_array($result)){ 
$search_array = array(
     "err"=>"","msg"=>"", 
     "hotel" => array(
      "att" => $search['hotel_id'], 
      "name" => $search['hotel_name'], 
      "city" => $search['city'], 
      "country" => $search['country']), 
     "city"=> array(
      "att"=> $search['hotel_id'], 
      "name" => $search['city'], 
      "country" => $search['country']) 
); 

array_push($json_array,$search_array); 
} 
echo json_encode($json_array); 
?> 

我這種變化,但只顯示1個記錄不是所有關鍵詞搜索類似

$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")'; 
$result=mysql_query($query);  

// Do Search 
$json_array = array(); 
while ($hasil=mysql_fetch_array($result)){ 

$search_array = array(
"err"=>intval("0"),"msg"=>"","data"=>array("f"=>intval("114"), 
"hotel"=>array(
$hotel_array = array(
      "att"=> $hasil['hotel_id'], 
      "name" => $hasil['hotel_name'], 
      "city" => $hasil['city'], 
      "country" => $hasil['country'] 
)), 
"city"=>array(

$city_array = array(
       "att"=> $hasil['hotel_id'], 
      "name" => $hasil['city'], 
      "city" => $hasil['city'], 
)) 

)); 

} 


echo json_encode($search_array); 

?> 

請幫助數組語法如何感謝

它類似的過程與本網站http://www.myhotelfinder.com/id/home/dohttp/predict?q=singapore

謝謝這個JSON輸出我的意思

<? 
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")'; 
$result=mysql_query($query);  

// Do Search 
$json_array = array(
    "err" => "", 
    "msg" => "", 
    "data" => array(
     "f" => 0, 
      "hotel" => array(), 
      "city" => array() 
    ) 
    ); 
    while ($hasil=mysql_fetch_array($result)){ 
     $hotel = array(
      "att" => $hasil['hotel_id'], 
      "name" => $hasil['hotel_name'], 
      "city" => $hasil['city'], 
      "country" => $hasil['country'] 
     ); 
     $city = array(
      "att"=> $hasil['hotel_id'], 
      "name" => $hasil['city'], 
      "country" => $hasil['country'] 
     ); 

     array_push($json_array["data"]["hotel"], $hotel); 
     array_push($json_array["data"]["city"], $city); 
    } 
    echo json_encode($json_array); 
    ?> 

吻^^

+0

這是什麼似乎是問題?每當你寫完'$ search_array'時,循環中的 – Ghost 2014-09-24 04:43:56

+0

就沒有意義了。但是在這個問題上你沒有正確描述這個問題。 – mithunsatheesh 2014-09-24 04:49:32

回答

0

我想您在詢問如何在while塊內格式化陣列以實現JSON輸出。

我想你可以嘗試以下數組聲明:

<? 
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")'; 
$result=mysql_query($query);  

// Do Search 
$json_array = array(); 
while ($search=mysql_fetch_array($result)){ 

$search_array = array(
    "err" => "", 
    "msg" => "", 
    "data" => array(
     "f" => 0, 
     "hotel" => array(
      "att" => $search['hotel_id'], 
      "name" => $search['hotel_name'], 
      "city" => $search['city'], 
      "country" => $search['country'] 
     ), 
     "city" => array(
      "att"=> $search['hotel_id'], 
      "name" => $search['city'], 
      "country" => $search['country'] 
     ) 
    ) 
); 


array_push($json_array,$search_array); 
} 
echo json_encode($json_array); 
?> 

UPDATE: 好吧,我開始看到一個更你可能會做什麼。請嘗試以下操作:

<? 
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")'; 
$result=mysql_query($query);  

// Do Search 
$json_array = array(
    "err" => "", 
    "msg" => "", 
    "data" => array(
     "f" => 0, 
     "hotel" => array(), 
     "city" => array() 
    ) 
); 
while ($hasil=mysql_fetch_array($result)){ 
    $hotel = array(
     "att" => $hasil['hotel_id'], 
     "name" => $hasil['hotel_name'], 
     "city" => $hasil['city'], 
     "country" => $hasil['country'] 
    ); 
    $city = array(
     "att"=> $hasil['hotel_id'], 
     "name" => $hasil['city'], 
     "country" => $hasil['country'] 
    ); 

    array_push($json_array["data"]["hotel"], $hotel); 
    array_push($json_array["data"]["city"], $city); 
} 
echo json_encode($json_array); 
?>