如果语句在 while 循环中
if statement in while loop
我有一个PHP脚本,可以从MySQL数据库中的表中获取一些数据。然后它使用 JSON javascript 将此信息发送回 PHP 页面。
这一切都很好用,但我想在 while 循环中做一个 if 语句。我的"已用"表发回"1"和"5",而不是回显 1 或 5,我希望它发送"是"或"否"。
一种方法可能是分解$data并将 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);
您可以在数组赋值中使用条件运算符:
'used' => $row['used'] == 1 ? 'Yes' : 'No',
您可以在没有语句的情况下执行此操作if
:
$answer = array(0 => 'No', 1 => 'Yes');
...
'used' => $answer[ $row['used'] ],
...
相关文章:
- 基于两个条件退出While循环
- 希望日期开始结束于while循环中的一个房间id的一个数组
- 为什么我的While循环不起作用
- 使用while循环+break是一种可接受的方式;goto;手术的结束
- 如何将while循环中生成的值存储到变量中,以便以后在JQuery中使用
- jquery,while循环在后台运行,同时while循环
- jquery悬停和while循环
- Javascript:用while循环在给定的count参数中重复一个字符串
- 如何修复 while 循环
- Jquery扩展点的宽度,同时通过while循环递增
- 斐波那契序列Javascript做while循环
- 为什么这个while循环不能使用AND逻辑运算符
- bluebird promise的while()循环正确(无递归?)
- JavaScript while循环没有'不能在有条件的情况下工作
- While循环在JavaScript中永远运行
- 逻辑(JSint友好)等效于while循环中的此赋值
- Javascript:用if语句运行while循环,用子句随机生成内容
- 如何使javascript在SQL while循环中回显所有数据
- 使用ajax循环while中的多个表单
- JavaScript 中的无限循环 - while 循环