2013-10-06 149 views
0

我有一個PHP腳本,從我的MySQL數據庫中的表中獲取一些數據。 然後它使用JSON javascript將這些信息發送回PHP頁面。如果語句在while循環

這一切都很好,但我想在while循環中做一個if語句。 我的「已用」表發送回「1」和「5」,而不是回顯1或5我希望它發送是或否。

這樣做的一種方法可能是打破$數據並用1或5替換爲是或否 但是還有其他方法嗎?

Something like: `if (used == '1') { set used == 'No } else {set used == 'Yes' }` 

我下面

while ($row = $result->fetch_assoc()) { 
    $data['results'][] = array(
     'id' => $row['id'], 
     'number' => $row['number'], 
     'type' => $row['type'], 
     'code' => $row['code'], 
     'price' => $row['price'], 
     'used' => $row['used'], 
     'utime' => $row['utime'], 
     'username' => $row['username'] 
    ); 
} 
//Everything went to plan so set success to true 
$data['success'] = true; 

//Set the content type for a json object and ensure charset is UTF-8. NOt utf8 otherwise it will not work in IE (Darn IE! >.<) 
header("Content-Type: application/json; charset=UTF-8"); 

//json encode the data and cast to an object so we can reference items like this.id in the javascript instead of this['id'] etc. 
echo json_encode((object)$data); 
+0

是你試圖存儲這些值以後使用?如果不是,只需在條件語句中回顯你想要的內容 – Sualkcin

回答

3

可以使用條件運算符的數組賦值代碼:

'used' => $row['used'] == 1 ? 'Yes' : 'No', 
+0

謝謝工作很好 –

0

你可以做到這一點沒有if聲明:

$answer = array(0 => 'No', 1 => 'Yes'); 

... 
'used' => $answer[ $row['used'] ], 
...