2013-10-06 150 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'] ], 
...