Tricky if statements...
Posted: Tue Mar 06, 2007 3:06 pm
Allo poppet. I gots me here a question about if statements. First here's the traditional code...
The code itself works fine, but the problem is, I've got like tons of these on the page. So what ends up happening is that if the first one goes off, it'll say "Hmm, it appears blah blah" AND it'll say "500 units bought correctly" (assuming the instance is the second, and not the first). Basically, I need to know how to stop that. I was thinking I'd just take away the "else" part, but then there'd be no error message at all.
What's a good way to avoid this?
Code: Select all
if ($foodamount1 == 1 && empty($foodamount2) && empty($foodamount3) && $barn == 'cardboard' && $money >= 500 && $currentfood == 0)
{
mysql_query("UPDATE user SET food = food+500 WHERE username = '$user'");
mysql_query("UPDATE user SET money = money-500 WHERE username = '$user'");
die("<b>500</b> units of food bought sucessfully.");
}
else
echo "Hmm, it appears you do not have enough money for the food, or your barn is full. ";
if ($foodamount1 == 1 && empty($foodamount2) && empty($foodamount3) && $barn == 'plywood' && $money >= 500 &&$currentfood >= 0 && $currentfood != 1000)
{
mysql_query("UPDATE user SET food = food+500 WHERE username = '$user'");
mysql_query("UPDATE user SET money = money-500 WHERE username = '$user'");
echo "<b>500</b> units of food bought sucessfully.";
}
else
echo "Hmm, it appears you do not have enough money for the food, or your barn is full. ";What's a good way to avoid this?