PDA

View Full Version : Needing Assistance In Shortning Some Code

Crash1hd
11-04-2003, 10:10 PM
I was wondering if anyone could help me with the following code would get way to long if I kept going this route I am sure there is a mathimatical way of making this work!
What I am trying to do is when someone enters a number there will be a value that goes with that number the value works on the following chart

-----------------------------------------
Pictures = 3.99
3Pictures = 9.99
10Pictures = 19.99
-----------------------------------------
here is the code below

Dim PicTotal
IF Request.Form("Muploadlimit") = "1" Then PicTotal = RSE.Fields("Pictures")
IF Request.Form("Muploadlimit") = "2" Then PicTotal = (RSE.Fields("Pictures")*2)
IF Request.Form("Muploadlimit") = "3" Then PicTotal = RSE.Fields("3Pictures")
IF Request.Form("Muploadlimit") = "4" Then PicTotal = RSE.Fields("3Pictures") + RSE.Fields("Pictures")
IF Request.Form("Muploadlimit") = "5" Then PicTotal = RSE.Fields("3Pictures") + (RSE.Fields("Pictures")*2)
IF Request.Form("Muploadlimit") = "6" Then PicTotal = (RSE.Fields("3Pictures")*2)
IF Request.Form("Muploadlimit") = "7" Then PicTotal = (RSE.Fields("3Pictures")*2) + RSE.Fields("Pictures")
IF Request.Form("Muploadlimit") = "8" Then PicTotal = (RSE.Fields("3Pictures")*2) + (RSE.Fields("Pictures")*2)
IF Request.Form("Muploadlimit") = "9" Then PicTotal = (RSE.Fields("3Pictures")*3)
IF Request.Form("Muploadlimit") = "10" Then PicTotal = RSE.Fields("10Pictures")
IF Request.Form("Muploadlimit") = "11" Then PicTotal = RSE.Fields("10Pictures") + RSE.Fields("Pictures")
IF Request.Form("Muploadlimit") = "12" Then PicTotal = RSE.Fields("10Pictures") + (RSE.Fields("Pictures")*2)
IF Request.Form("Muploadlimit") = "13" Then PicTotal = RSE.Fields("10Pictures") + RSE.Fields("3Pictures")
IF Request.Form("Muploadlimit") = "14" Then PicTotal = RSE.Fields("10Pictures") + RSE.Fields("3Pictures") + RSE.Fields("Pictures")
IF Request.Form("Muploadlimit") = "15" Then PicTotal = RSE.Fields("10Pictures") + RSE.Fields("3Pictures") + (RSE.Fields("Pictures")*2)
IF Request.Form("Muploadlimit") = "16" Then PicTotal = RSE.Fields("10Pictures") + (RSE.Fields("3Pictures")*2)
IF Request.Form("Muploadlimit") = "17" Then PicTotal = RSE.Fields("10Pictures") + (RSE.Fields("3Pictures")*2) + RSE.Fields("Pictures")
IF Request.Form("Muploadlimit") = "18" Then PicTotal = RSE.Fields("10Pictures") + (RSE.Fields("3Pictures")*2) + (RSE.Fields("Pictures")*2)
IF Request.Form("Muploadlimit") = "19" Then PicTotal = RSE.Fields("10Pictures") + (RSE.Fields("3Pictures")*3)
IF Request.Form("Muploadlimit") = "20" Then PicTotal = (RSE.Fields("10Pictures")*2)
IF Request.Form("Muploadlimit") = "21" Then PicTotal = (RSE.Fields("10Pictures")*2) + RSE.Fields("Pictures")
Response.Write " @ \$"&PicTotal
SubTotal = SubTotal + PicTotal

oracleguy
11-04-2003, 11:12 PM
You could do a Select Case so that it only has to retrieve the value from the Request.Form() collection once. It may speed it up (not that you'd notice...) but I dunno how much shorter it could be.

Crash1hd
11-04-2003, 11:33 PM
I guess what I am lookiong for is a math equation that would work for any number where X = y or something

M@rco
11-05-2003, 12:27 AM
Apart from the fact that that's a *crazy* pricing system (e.g. 9 units = 29.97, whereas 10 are 19.99), here's what I've cooked up for you:
Function GetCost(Num)
Dim Remainder

GetCost = (Num \ 10) * 19.99
Remainder = Num MOD 10

GetCost = GetCost + ((Remainder \ 3) * 9.99)
Remainder = Remainder MOD 3

GetCost = GetCost + (Remainder * 3.99)
End Function

Output:
GetCost(1) = 3.99
GetCost(2) = 7.98
GetCost(3) = 9.99
GetCost(4) = 13.98
GetCost(5) = 17.97
GetCost(6) = 19.98
GetCost(7) = 23.97
GetCost(8) = 27.96
GetCost(9) = 29.97
GetCost(10) = 19.99
GetCost(11) = 23.98
GetCost(12) = 27.97
GetCost(13) = 29.98
GetCost(14) = 33.97
GetCost(15) = 37.96
GetCost(16) = 39.97
GetCost(17) = 43.96
GetCost(18) = 47.95
GetCost(19) = 49.96
GetCost(20) = 39.98
GetCost(21) = 43.97It should go without saying (but I'll say it anyway) that I present this code on the assumption that you will spend time understanding the code and brushing up on your VBScript operators so that you could do it yourself next time...

;)

Crash1hd
11-05-2003, 01:10 AM
Of course! I was trying to give people a deal so that if they where to buy more they get a better deal! But I do see what you mean by really wiered pricing! Any thoughts people?

M@rco
11-06-2003, 12:38 AM
Why not simply offer a compound discount (like compound interest)?

Something like this:
Cost = (Units * BaseRate) * (0.99^Units)

The exact value will need tweaking... ;)