0

MySQL データベースのテーブルからデータを取得する PHP スクリプトがあります。そして、JSON JavaScript を使用してこの情報を PHP ページに送り返します。

それはすべてうまくいきますが、whileループでifステートメントを作成したいと思います。私の「使用済み」テーブルは、1 または 5 をエコーバックする代わりに「1」と「5」を返信します。Yes または NO を送信したいのです。

これを行う 1 つの方法は、$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);
4

2 に答える 2

3

配列の割り当てで条件演算子を使用できます。

'used' => $row['used'] == 1 ? 'Yes' : 'No',
于 2013-10-06T00:54:00.537 に答える