437,949 Members | 1,827 Online Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

# 2 dumb questions

 P: n/a First, I'm sure the following code is not using an efficient structure for the nested if then else's but I can't recall the right way: if (\$item_qty 19) { \$price = (\$price * .925); \$qty_discount = '20 @ 7.5%'; } else { if (\$item_qty 9) { \$price = (\$price * .95); \$qty_discount = '10 @ 5%'; } else { if (\$item_qty 4) { \$price = (\$price * .975); \$qty_discount = '5 @ 2.5%'; } } } Second question, if you see what I'm doing it's to discount e-commerce sales based on the quanity ordered. It's easier to pull 50 of the same item than 1 each of 50 different things. I'm not sure what's quite right but lets say an order of 1 has no discount and an order of 100 is 50% off. Surely there is a way of incrementally calculating the discount for any quantity between 1 and 100 but I'm no math whiz. hmmm... maybe for that one, since 50% * 2 is 100 then: \$item_qty = 100 \$price = \$100 \$qty_discount = (\$item_qty/2) [50(%)] \$price = (\$price - (\$price * (\$qty_discount / 100))) [\$50] \$100 - \$100 * 50% Um, damn, I'm lost... Mar 2 '07 #1
4 Replies

 P: n/a Paul Furman wrote: First, I'm sure the following code is not using an efficient structure for the nested if then else's but I can't recall the right way: if (\$item_qty 19) { \$price = (\$price * .925); \$qty_discount = '20 @ 7.5%'; } else { if (\$item_qty 9) { \$price = (\$price * .95); \$qty_discount = '10 @ 5%'; } else { if (\$item_qty 4) { \$price = (\$price * .975); \$qty_discount = '5 @ 2.5%'; } } } Second question, if you see what I'm doing it's to discount e-commerce sales based on the quanity ordered. It's easier to pull 50 of the same item than 1 each of 50 different things. I'm not sure what's quite right but lets say an order of 1 has no discount and an order of 100 is 50% off. Surely there is a way of incrementally calculating the discount for any quantity between 1 and 100 but I'm no math whiz. I solved this part so the first problem doesn't really matter though I should know how to do that! Basically I made this 'rate' number that says how much to increment per unit purchased. Each additional unit gets a bit more discount. \$item_qty = 10; \$price = 100; //\$1-\$100 discount up to 50% \$rate = (.5/100); //subtract one 'rate' so a purchase of 1 unit is not discounted .005% (\$qty_discount = ((\$item_qty*\$rate)/1)-\$rate); //set cap at 50% if (\$item_qty 99){\$qty_discount = .5;} //subtract discount from price \$price = (\$price - (\$price * (\$qty_discount))); print " final: ".\$price; ~returns \$50 discounted price at 100 units~ Mar 3 '07 #2

 P: n/a Paul Furman kirjoitti: First, I'm sure the following code is not using an efficient structure for the nested if then else's but I can't recall the right way: With the help of our friend "else if" you get a really clean code: if (\$item_qty 19) { \$price = (\$price * .925); \$qty_discount = '20 @ 7.5%'; } else if (\$item_qty 9) { \$price = (\$price * .95); \$qty_discount = '10 @ 5%'; } else if (\$item_qty 4) { \$price = (\$price * .975); \$qty_discount = '5 @ 2.5%'; } -- "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg) Mar 3 '07 #3

 P: n/a Kimmo Laine wrote: Paul Furman kirjoitti: >First, I'm sure the following code is not using an efficient structurefor the nested if then else's but I can't recall the right way: With the help of our friend "else if" you get a really clean code: if (\$item_qty 19) { \$price = (\$price * .925); \$qty_discount = '20 @ 7.5%'; } else if (\$item_qty 9) { \$price = (\$price * .95); \$qty_discount = '10 @ 5%'; } else if (\$item_qty 4) { \$price = (\$price * .975); \$qty_discount = '5 @ 2.5%'; } Thanks, that's better. I should know this stuff... Mar 3 '07 #4

 P: n/a Paul Furman wrote: >Second question, if you see what I'm doing it's to discount e-commercesales based on the quanity ordered. It's easier to pull 50 of the sameitem than 1 each of 50 different things. I'm not sure what's quiteright but lets say an order of 1 has no discount and an order of 100is 50% off. Surely there is a way of incrementally calculating thediscount for any quantity between 1 and 100 but I'm no math whiz. I solved this part.. This is a little better: \$price = \$r["price"] / 100; \$full_price = \$price / 100;//reserve for calculating discounts \$item_qty = \$row["item_qty"]; //quantity discount \$limit = 50;//no further discount above this qty \$max_disc = .3;//max discount \$rate = (\$max_disc/\$limit);//discount per unit above 1 //subtract one 'rate' so a purchase of 1 unit is not discounted (\$qty_discount = ((\$item_qty*\$rate)/1)); //set cap if (\$item_qty (\$limit-1)){\$qty_discount = \$max_disc;} //subtract qty discount from price \$price = (\$price - (\$price * (\$qty_discount))); then: //buyer discount if (\$discount != 0){//if any buyer discount if ((\$discount-(\$qty_discount*100))>0){//if not already discounted above qty disc //reduce buyer discount by amount of quantity disc \$adj_discount = (\$discount-(\$qty_discount*100)); \$price = (\$price - (\$full_price * (\$adj_discount / 100))); } else {\$adj_discount = 0;} } Mar 3 '07 #5

### This discussion thread is closed

Replies have been disabled for this discussion. 