Page 1 of 1

Division by zero!

Posted: Thu Jul 08, 2004 4:35 pm
by Joe
Does anyone know why I have a division by zero in this code:

Code: Select all

while (true)
{
 $row = mysql_fetch_assoc($result);
 if ($row == false) break;
 $name = $row['name'];
 $name1 = $_POST[$name];
 $price = $row['price'];
 $grams = $name1 / $price;

 if ($name1 > '0')
 {
  echo "$name = $$name1 | $grams grams<br>";
  echo "<input type='hidden' name='$name' value='$name1'>"; 
 }
}
Regards


Joe 8)

Posted: Thu Jul 08, 2004 4:45 pm
by PrObLeM
$price == 0....

because either the price is 0 or its not the query isnt returning anything

Posted: Thu Jul 08, 2004 4:49 pm
by Joe
So what would be the best way to work around this problem. By using the $price == 0 in my code somewhere or do I have to rewrite to something different :)

Regards


Joe 8)

Posted: Thu Jul 08, 2004 5:03 pm
by PrObLeM

Code: Select all

$grams = ($price <> 0)? $name1 / $price : 'Price is 0';

Posted: Thu Jul 08, 2004 5:08 pm
by Joe
Yeah thanks alot. I changed it around a little as your method didnt seem to work but you gave me an insight:

if($price <> 0)
{ $grams = $name1 / $price; }

Thanks man!

Posted: Thu Jul 08, 2004 5:12 pm
by PrObLeM
$grams = ($price <> 0)? $name1 / $price : 'Price is 0';
should work....