Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
December 23rd, 2012, 08:27 AM
 Rhytz
Contributing User

Join Date: Jan 2011
Posts: 104
Time spent in forums: 1 Day 11 h 53 m 30 sec
Reputation Power: 50
Calculate how many times a number fits in another

Hi guys,

First of all, happy holidays

What is the cleanest way to calculate how many times a number fits into another number with PHP?

For example, my script will have 2 variable numbers let's say: 14 and 33. So 14 will fit twice in 33.

What is the best way to calculate this? I also need to know how much remains.

I was thinking of doing this in a loop, but that doesnt seem optimal.

Any thoughts?

#2
December 23rd, 2012, 08:56 AM
 Triple_Nothing
Contributing User

Join Date: Jun 2009
Posts: 507
Time spent in forums: 5 Days 18 h 37 m 35 sec
Reputation Power: 7
Just make a basic request for what you desire. The whole number:
PHP Code:
```  \$total = (int) (\$a / \$b);  ```

And the remainder:
PHP Code:
```  \$remain = (\$a % \$b);  ```

Math function start at basic places:
http://php.net/manual/en/language.o....arithmetic.php
http://php.net/manual/en/ref.math.php

#3
December 23rd, 2012, 09:04 AM
 Jacques1
You have been warned

Join Date: Jul 2012
Posts: 3,096
Time spent in forums: 2 Months 3 Weeks 4 Days 7 h 4 m 44 sec
Reputation Power: 1063
Hi,

this is called integer division. Unfortunately, PHP doesn't have that built in, so you have to write it yourself:
PHP Code:
``` echo (int) (33/14);  ```

(Using round() or floor() yields the incorrent result for negative numbers).

The remainder is calculated with the % operator:
PHP Code:
``` echo 33 % 14;  ```

#4
December 23rd, 2012, 11:03 AM
 Rhytz
Contributing User

Join Date: Jan 2011
Posts: 104
Time spent in forums: 1 Day 11 h 53 m 30 sec
Reputation Power: 50
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 11:06 AM.

 Viewing: Dev Shed Forums > Programming Languages > PHP Development > Calculate how many times a number fits in another