Forum Moderators: coopster

Message Too Old, No Replies

If Statements

If value == true then do THIS else do THAT

         

ski442

3:15 am on Dec 9, 2009 (gmt 0)

10+ Year Member



Hi Guys.
Not very good when it come to these, I have been on this for about five hours and this is what i have so for.

if ($row["onoffer"]==true )
{
$totalCost += (row["qty"] * $row["onffer"]);
}
else
{
$totalCost += (row["qty"] * $row["price"]);}
?>

Both "Price" & "onffer" come from the same DB table, which relates to one item, now i have 500+ items in the db and ALL have a "price" and only some have a "onoffer" price.

So the code above is from a cart script which i have been playing with, all works untill the widget is on offer, well it still works just does not even know the widget is on offer, so have been trying to write the code above, so help from the pros is most needed, just can't work out the correct order in which the layout.
I can post rest of script if needed.

Thanks very much in advance
Ski442

rocknbil

4:18 am on Dec 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am puzzled by this statement. :-)

well it still works just does not even know the widget is on offer

First, you have an error here: row["qty"] s/b $row["qty"]

At any rate, look at this:

if ($row["onoffer"]==true )

This appears to be telling me that the field onoffer is a boolean true/false field. Right? So if the possible values are true or false, what do you get when you do this?

$totalCost += ($row["qty"] * $row["onffer"]);

Quantity * true is . . . zero. Math computations on strings will always evaluate to zero (or -1 in some languages . . .)

You can test this with a simple select:

select price * onoffer from table limit 20;

You will get 0.00 down the line, for both true and false.

Here, I think, is the way you should do this: you have two decimal fields:

create table test (id int(11) primary key auto_increment,
price decimal (12,2) default '0.00',
onoffer decimal (12,2) default '0.00');

If you decide to put an item on sale (which is what I'm guessing onoffer means,) just add the value to that column. You don't even need to delete price. So you might have

id¦price¦onoffer
1234¦12.99¦10.99

select * from table;

extract the rows,

$price=$row['price'];
$onoffer=$row['onoffer'];

then you can do this:

$final_price = ($onoffer > 0)?$onoffer:$price;

The previous is a short circuit evaluation, and is identical to this:
if ($onoffer >0) { $final_price = $onoffer; }
else { $final_price = $price; }

Now you don't care which one you calculate,

$totalCost += $row["qty"] * $final_price;

This also allows you to do things like this:

if ($onoffer > 0) {

$diff = $price - $onoffer;
echo "<p style=\"color:red;font-weight:bold;\">Special offer: buy now for \$$onoffer ,\$$diff off the regular price of \$$price!</p>";

}

Dollar signs escaped, you will get

<p style="color:red;font-weight:bold;">Special offer: buy now for $10.99 ,$2.00 off the regular price of $12.99!</p>

Will have to do formatting to get decimal "2.00", but one thing at a time . . .

ski442

10:52 am on Dec 9, 2009 (gmt 0)

10+ Year Member



Hi Rocknbil.

Sorry about the confused bit at the start, what i should of said was, the whole script works fine with the just the single "price", but need to add in and "offerprice". Sorry.

But You are so fast and you understood exactly what i needed and your script works like a charm my friend, I can do the 2 decimal format I think, then you have answered the next part which I was about the work on today was the $diff.

How do you just come up with it like that, Brill.
Thanks very much for you help.
Ski442

rocknbil

8:54 pm on Dec 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How do you just come up with it like that

I didn't,

. . . many late nights over many moons, with wife steering clear of the "computer room", worrying if something was going to fly in her direction . . .

. . . in your case, I've done exactly this, with sale pricing . . .

. . . forgotten more than many people will probably (want to!) know, and posts like these help remind me, which is why I participate, selfish b*****d that I am! :-)

ski442

9:29 pm on Dec 9, 2009 (gmt 0)

10+ Year Member



I know what you mean, my wife the same.

I do have more questions on the php math but I need to do some research first. Just so i cab try to under stand.

Thanks very much for your time.
See you on the next chapter.
Thanks
Ski442