
December 23rd, 2012, 10:03 AM
|
 |
Contributing User
|
|
|
|
Thanks guys. Im building an online shop for dutch holiday foods, with special deals on large quantities (bags).
But these bags can have variable quantities in them. So lets say we have this:
Bag of 20
Bag of 10
And a order quantity of 32. The bag of 20 fits in once and the bag of 10 fits in once, and we'd have 3 single ones left. I was thinking of coding it like this:
PHP Code:
<?php
$amount = 33; //Variable
$product_id = 1; //Variable
$discount_query = dbquery("SELECT * FROM products WHERE product_sub='1' AND product_subid='".$product_id."' AND product_mq<='".$amount."' ORDER BY product_mq DESC"); //Select the discount bags from the database
while($discount_data = dbarray($discount_query)){ //Loop through bags, from high quantity to low
//$total = (int) ($a / $b);
$discountbags[$discount_data['product_id']] = (int) ($amount / $discount_data['product_mq']); //Count how many wholesale bags of the current type and product id fit in the current amount
$amount = ($amount % $discount_data['product_mq']); //Update the amount for the next iteration
}
print_r $discountbags; //How many wholesale bags of different types fit in the current amount
?>
I havent tested it yet since my system is pretty complicated, but would this be a correct way to do it?
Last edited by Rhytz : December 23rd, 2012 at 10:06 AM.
|