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
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Thu Jul 08, 2004 4:35 pm
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
PrObLeM
Forum Contributor
Posts: 418 Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:
Post
by PrObLeM » Thu Jul 08, 2004 4:45 pm
$price == 0....
because either the price is 0 or its not the query isnt returning anything
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Thu Jul 08, 2004 4:49 pm
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
PrObLeM
Forum Contributor
Posts: 418 Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:
Post
by PrObLeM » Thu Jul 08, 2004 5:03 pm
Code: Select all
$grams = ($price <> 0)? $name1 / $price : 'Price is 0';
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Thu Jul 08, 2004 5:08 pm
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!
PrObLeM
Forum Contributor
Posts: 418 Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:
Post
by PrObLeM » Thu Jul 08, 2004 5:12 pm
$grams = ($price <> 0)? $name1 / $price : 'Price is 0';
should work....