Need help with a calculation...

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
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Need help with a calculation...

Post by michlcamp »

I've got a shipping calculation that goes like this:

Code: Select all

Shipping: <?php if ($total > 81.75) {printf ("%01.2f", $total * .11);}else {echo "8.99 ";}?>
but I need to add another calculation, IF the order is from a country other that the U.S. - something like,

if ($country is NOT USA, * .25)

I still need the other two parts to work the same as they have been if the order is from the U.S, but need to multiply $total by 25% if outside of the US, regardless of $total...

so, in non-php terms... IF $country is USA, AND $total > 81.75, printf $total * .11,

but IF $ country is NOT USA, printf $total * .25

also needs to go into a hidden field to pass to the next page...

Code: Select all

<input type="hidden" name="shipping" value="<?php if ($total > 81.75) {printf ("%01.2f", $total * .11);} else {echo "8.99 ";}?>">
Don't know how to write multiple IFs...
Any/all help appreciated...
thanks in advance.
mc
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

just do somtin like

Code: Select all

<?php
if ($country == 'USA')
{
    if ($total > 81.75)
        $show =  $total * 0.11;
    else 
        $show = 8.99;
}
else
{
    if ($total > 81.75)
        $show =  $total * 0.11 * 0.25;
    else 
        $show = 8.99 * 0.25;
}
?>

<input type="hidden" name="shipping" value="<?= $show ?>">
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

For the sake of debugging, I'd really recommend splitting up if conditional onto multiple lines
Post Reply