#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    105
    Rep Power
    51

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep 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.op...arithmetic.php
    http://php.net/manual/en/ref.math.php
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    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
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    105
    Rep Power
    51
    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.

IMN logo majestic logo threadwatch logo seochat tools logo