1

データベース内の行数をカウントし、結果をユーザーに返す次のコードがあります。

// Connect to database and select
$mysqli = mysqli_connect($config['db_hostname'], $config['db_username'], $config['db_password'], $config['db_name']);
$mysqli->set_charset('utf8');
$select = 'SELECT COUNT(*) AS wines FROM  ft_form_4 WHERE feu_id = "'.$feuid.'"';
$response = mysqli_query($mysqli, $select) or die("Error: ".mysqli_error($mysqli));
$result = mysqli_fetch_array($response, MYSQLI_ASSOC);

$wines = (int)$result[wines];

echo $wines;

// Return 0 for no entries
if ($wines = 0) {

echo 'You currently have no wines entered, your current total entry fee is <strong>£0</strong>.';
}

elseif ($wines = 1) {

echo 'You currently have 1 wine entered, your current total entry fee is <strong>£135</strong>.';
}

elseif ($wines > 1) {

$fee = $wines * 135;
echo 'You currently have '.$wines.' wines entered, your current total entry fee is <strong>'.$fee.'</strong>.';
}

コードを実行すると、結果は最初のビット (テストのために入れたばかり) で 3 としてエコーされますが、これは正しいですが、常に 2 行目が表示され、入力されたワインが 1 つあり、エントリ料金が £ であることが示されます。 135。3 を数字として認識していないようです。

mysqli_num_rows を使用して 2 番目のコードを試しましたが、それもうまくいきません。

4

5 に答える 5

1

多分よりスマートなアプローチ?

$fee = $wines * 135;
echo 'You currently have '.($wines > 0 ? $wines : 'no').' wine'.($wines != 1 ? 's' : '').' entered, your current total entry fee is <strong>£'.$fee.'</strong>.';

説明:

($wines > 0 ? $wines : 'no')

括弧内で行うことは、最初の部分を評価することです。

$wines > 0

その場合、「?」の後の値は (if) が出力され、false の場合は ':' (else) の後の値が出力されます。

同様に、wine* s *で s をエコーするかどうかを調べます。

($wines != 1 ? 's' : '')

$wines == 1 の場合、wine* s *の s をエコーし​​ません:

于 2013-10-25T14:03:27.657 に答える
0

比較が間違っています。==代わりに使用する必要があります=

于 2013-10-25T14:04:13.117 に答える