if then statements

if($qty>1)
{
$_SESSION[‘shipping_rate’]=3+(($qty-1)*(2.25));
}
else
{
$_SESSION[‘shipping_rate’]=3;
}

i would like to expand the if statement to multiple level of qty to added shipping extra charge

now it adds 2.25 to each qty of product over 1 item

i would like to scale it up to

if qty=2 add 2.25
if qty=3 add 2.20
if qty=4 add 2.10
if qty>5 multiply ship rate * .25

thank you

It appears you already know what you need to do, so what is your question?

You should look into doing a switch statement…

Doing it with if statements below (just add the other conditions)

[php]if($qty==2)
{
$_SESSION[‘shipping_rate’]=3+(($qty-1)(2.25));
}
elseif ($qty==3)
$_SESSION[‘shipping_rate’]=3+(($qty-1)
(2.20));
else
{
$_SESSION[‘shipping_rate’]=3;
}[/php]

http://php.net/manual/en/control-structures.switch.php

with a switch statement

[php]switch ($qty) {
case 2:
$_SESSION[‘shipping_rate’]=3+(($qty-1)(2.25));
break;
case 3:
$_SESSION[‘shipping_rate’]=3+(($qty-1)
(2.2));
break;
case 4:
$_SESSION[‘shipping_rate’]=3+(($qty-1)(2.1));
case >= 5:
$_SESSION[‘shipping_rate’]=3+(($qty-1)
(.25));
default:
$_SESSION[‘shipping_rate’]=3;
}[/php]

Hi thanks for all the replies

None of those replies are working

it breaks the page

the original code i posted works but does not give me multiple controlled shipping add options

anyone else have a solution

Breaks the page how? Are you copying and pasting the reply in and expecting it to work or are you modifying it to suit what you need?

Yes copying just the code in place of the original code I supplied

Okay, so fix what is broken. Anywhere you look you are going to get the same responses for your problem. The sad thing is, you gave what you needed to do in your first post and are complaining that code given to you doesn’t work when you copy it over, you code do the coding to fix it.

Not complaining

I’m here because I don’t know what I’m Doin

And asking for help

I gave a a section of code that works and a scenario of change that I asked for help on
What code adjustment I need to happen to change the code

I got offers of code that I am very thankful for
And they didn’t work
I’m just stating that

And asking for a solution that you obviously are not providing
And critiquing all my replies

Compare the above with this:

And tell me what differences you see?

Granted I am one of the resident pricks here, I don’t provide “solutions,” I will give guidance. You don’t learn when an answer is handed to you. Combined with the fact that you are literally copying and pasting wanting a complete answer means that you are not learning anyway.

I did make a couple of mistakes in my statement, I corrected them below.

And we all love [member=72272]astonecipher[/member] :slight_smile:

Used the examples, I provided and scale them up as Astonecipher said, follow what I did and add in the missing elseif statements.

[php] if($qty==2)
{
$_SESSION[‘shipping_rate’]=3+(($qty-1)(2.25));
}
elseif ($qty==3)
{
$_SESSION[‘shipping_rate’]=3+(($qty-1)
(2.20));
}
else
{
$_SESSION[‘shipping_rate’]=3;
}
[/php]

[php] switch ($qty) {
case 2:
$_SESSION[‘shipping_rate’]=3+(($qty-1)(2.25));
break;
case 3:
$_SESSION[‘shipping_rate’]=3+(($qty-1)
(2.2));
break;
case 4:
$_SESSION[‘shipping_rate’]=3+(($qty-1)(2.1));
break;
case >= 5:
$_SESSION[‘shipping_rate’]=3+(($qty-1)
(.25));
break;
default:
$_SESSION[‘shipping_rate’]=3;
}[/php]

Thanks Everyone,

This is what i came up with

if($qty==2) $_SESSION[‘shipping_rate’]=6;
elseif ($qty==3) $_SESSION[‘shipping_rate’]=8.5;
elseif ($qty==4) $_SESSION[‘shipping_rate’]=9;
elseif ($qty>4) $_SESSION[‘shipping_rate’]=3+(($qty-1)*(1.15));
else $_SESSION[‘shipping_rate’]=3;

it works now

ill try and figure out why the other code from Topcoder didnt work (i do like learning)

Good job. Now for the lesson on the switch case that didn’t work:
[php]

<?php print "This is a test of the Switch Statement
"; for ($i = 1; $i < 10; $i++) { switch ($i) { case ($i <= 4): echo "$i less than 5
"; break; case 5: echo "$i == 5
"; break; case ($i > 5): echo "$i greater than 5
"; break; default: echo "$i == $i
"; } } [/php] Output:
This is a test of the Switch Statement 1 less than 5 2 less than 5 3 less than 5 4 less than 5 5 == 5 6 greater than 5 7 greater than 5 8 greater than 5 9 greater than 5
Generally, a switch is for evaluating a single value, not an expression in a case statement. So, [php]case >= 5:[/php] gives the same problem you would get when doing, [php]if ( >= 5 )[/php] The fix is adding the variable that you are actually checking.
Sponsor our Newsletter | Privacy Policy | Terms of Service