gpt4 book ai didi

javascript - while循环中的if语句

转载 作者:太空宇宙 更新时间:2023-11-03 12:22:30 24 4
gpt4 key购买 nike

我有一个 PHP 脚本,可以从我的 MySQL 数据库中的表中获取一些数据。然后它使用 JSON javascript 将此信息发送回 PHP 页面。

一切都很好,但我想在 while 循环中做一个 if 语句。我的“已用”表发回“1”和“5”,而不是回显 1 或 5,我希望它发送"is"或“否”。

执行此操作的一种方法是分解 $data 并将 1 或 5 替换为 Yes 或 No。但是还有其他方法吗?

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

最佳答案

可以在数组赋值中使用条件运算符:

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

关于javascript - while循环中的if语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19204265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com