views:

128

answers:

3

I am trying to echo €00.00 if my variable $amount equals zero

I have done something wrong can you help??

Code

while ($row9 = mysql_fetch_array($result9))
{
$amount = $row9['amount'];
}
//$amount = $amount / 60;
//$amount = round($amount, 2);
if $amount == 0 echo "<b>Balance: &#8364;00.00</b>";
else
echo "<b>Balance: $$amount</b>";
+1  A: 

You need to put the if/else in the loop, and you have some invalid syntax (missing parens and double $). So:

while ($row9 = mysql_fetch_array($result9))
{
  $amount = $row9['amount'];
  if ($amount == 0)
  { 
    echo "<b>Balance: &#8364;00.00</b>";
  }
  else
  {
    echo "<b>Balance: $amount</b>";
  }
}
Matthew Flaschen
Why the downvote?
Matthew Flaschen
+1  A: 

You are adding extra $ to the $amount, try this:

if ($amount == 0) {
   echo "<b>Balance: &#8364;00.00</b>";
} else {
  echo "<b>Balance: $amount</b>";
}

In fact you can make your code a bit more readable/standard like this:

if ($amount == 0)
{
  echo "<b>Balance: &#8364;00.00</b>";
}
else
{
  echo "<b>Balance: $amount</b>";
}
Web Logic
The parentheses around the condition are required. It's not a question of readability.
Matthew Flaschen
@Matthew Flaschen: I have already suggested that to OP.
Web Logic
The parentheses are still missing the in the first code example.
waiwai933
@waiwai933: I had corrected an error in the OP's code, posted his code there and **that is the reason** later I suggested him a better coding style.
Web Logic
@Web No doubt. Nevertheless, you still were suggesting that he use the first example, which was the reason we were pointing that there were no parentheses.
waiwai933
In your last example, if the database returns `'0.00'` for `amount`, `if (!$amount)` will evaluate as `false`.
webbiedave
@webbiedave: right, i removed that though.
Web Logic
A: 

I've moved the if statement inside the while loop, cleaned up the code and removed the extra $ sign that was on the last line.

while ($row9 = mysql_fetch_array($result9)) {
    if ($row9['amount']) == 0 {
        echo "<b>Balance: &#8364;00.00</b>";
    } else {
        echo "<b>Balance: $row9['amount']</b>";
    }    
}
Pheter