Array? Dictionary Object?
Hey guys!
I am looking for a slick no-nonsense way to accomplish the following.
I have a couple of fields in a DB. I did not create the DB or this would be handled differently. The fields are NumberForDiscount and DiscountPercent.
It is supposed to work like this:
A user purchases a number of widgets I get the values from the database. Let's say that NumberForDiscount="5,10,15"
and DiscountPercent="10,15,20"
This would mean that if the user purchases at least 5 widgets
they will receive a 10% discount 10 or more widgets they would get a 15% discount, 15 or more widgets they would get a 20% discount.
I am not sure how I want to handle this any suggestions? It seems really messy. if it were up to me I would change the DB structure, but that cannot be done.
Thanks,
Mike
Blessed is the man who fears the LORD, who delights greatly in his commandments. Psalms 112:1
mycoolross posted this at 17:20 — 27th December 2002.
They have: 82 posts
Joined: Oct 2001
By the way I forgot to mention this is ASP. I guess you can figure that out with the use of the dictionary object.
- Mike
ROB posted this at 17:25 — 27th December 2002.
They have: 447 posts
Joined: Oct 1999
this will surely need some tweaking, but you should get the idea
<?php
//assuming you already pulled these values from the database
$NumberForDiscount = \"5,10,15\";
$DiscountPercent = \"10,15,20\";
$productcost = '3.99';
// and this would be determined by what the customer is purchasing
$purchasequantity = 7;
// make them arrays
$quantities = explode($NumberForDiscount, ',');
$percentages = explode($DiscountPercent, ',');
$thisdiscountpercent = 0;
for($i=0; $i<sizeof($quantities); ++$i) {
if($purchasequantity >= $quantities[$i]) $thisdiscountpercent = $percentages[$i];
else break;
}
// determine the pre-discount total
$thispurchaseamount = $productcost * $purchasequantity;
// determine the discount
$thisdiscountamount = $thispurchaseamount * ($thisdicountpercent / 100);
// subtract the discount
$thispurchaseamount -= $thisdiscountamount;
?>
[edit]DOH! NOT ASP!!!! Sorry i won't be of much help there[/edit]
mycoolross posted this at 17:29 — 27th December 2002.
They have: 82 posts
Joined: Oct 2001
Rob,
That is great! But I need ASP code. I appreciate your quick response.
Besides that any code that has an explode() function scares me.
- Mike
ROB posted this at 17:39 — 27th December 2002.
They have: 447 posts
Joined: Oct 1999
yeah i realized that after i posted. im afraid i can be of little help there, but if ASP has a similar method to explode() to split a string into an array, converting the above to ASP should be fairly straight-forward.
cheers
Mark Hensler posted this at 20:23 — 27th December 2002.
He has: 4,048 posts
Joined: Aug 2000
I translated the above PHP to ASP. I did this really fast.... hope it works.
<%
'assuming you already pulled these values from the database
Dim strNumberForDiscount = "5,10,15"
Dim strDiscountPercent = "10,15,20"
Dim fltProductCost = 3.99
'and this would be determined by what the customer is purchasing
Dim intPurchaseQuantity = 7
'make them arrays
Dim arrQuantities = Split(strNumberForDiscount, ",")
Dim arrPercentages = Split(strDiscountPercent, ",")
Dim fltThisDiscountPercent = 0
Dim i
For i=0 to UBound(arrQuantities)
If intPurchaseQuantity >= intQuantities(i) Then
fltThisDiscountPercent = arrPercentages(i)
Else
Break
End If
Next
'determine the pre-discount total
fltThisPurchaseAmount = fltProductCost * intPurchaseQuantity
'determine the discount
fltThisDiscountAmount = fltThisPurchaseAmount * (ThisDiscountPercent / 100)
'subtract the discount
fltThisPurchaseAmount = fltThisPurchaseAmount - fltThisDiscountAmount
%>
Mark Hensler
If there is no answer on Google, then there is no question.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.