Division by zero!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Division by zero!

Post 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)
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

$price == 0....

because either the price is 0 or its not the query isnt returning anything
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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)
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

Code: Select all

$grams = ($price <> 0)? $name1 / $price : 'Price is 0';
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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!
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

$grams = ($price <> 0)? $name1 / $price : 'Price is 0';
should work....
Post Reply