PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 23rd, 2012, 07:27 AM
Rhytz's Avatar
Rhytz Rhytz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 100 Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 11 h 4 m 50 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?

Reply With Quote
  #2  
Old December 23rd, 2012, 07:56 AM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 294 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 5 m 6 sec
Reputation Power: 5
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

Reply With Quote
  #3  
Old December 23rd, 2012, 08:04 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,837 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 5 h 22 m 55 sec
Reputation Power: 811
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

Reply With Quote
  #4  
Old December 23rd, 2012, 10:03 AM
Rhytz's Avatar
Rhytz Rhytz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 100 Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 11 h 4 m 50 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 10:06 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Calculate how many times a number fits in another

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap