butsags Posted August 28, 2010 Share Posted August 28, 2010 Hey guys, So i have a variable that i want to check. I want the script to check if a number is over 150 then break it down into multiple numbers each up to 150. so if the number is 900, there will be six chunks of 150. if the number is say 800, there will be 5 chunks of 150 and one chunk of 50. etc something like: <?php if($OrderWeight > 150){ $OrderWeight = explode("150", '$OrderWeight'); $i=0; while ($i < $OrderWeight) { $OrderWeight[$i]; } } else { } ?> Thanks Guys. I feel like this is a very simple equation, I'm just missing a simple php function Quote Link to comment https://forums.phpfreaks.com/topic/211920-simple-math-equation-help/ Share on other sites More sharing options...
DavidAM Posted August 28, 2010 Share Posted August 28, 2010 $chunks = int($OrderWeight / 150); $leftover = $OrderWeight % 150; Quote Link to comment https://forums.phpfreaks.com/topic/211920-simple-math-equation-help/#findComment-1104513 Share on other sites More sharing options...
butsags Posted August 28, 2010 Author Share Posted August 28, 2010 Thanks for your reply man, im getting an error on $chunks = int($OrderWeight / 150); I think because int cant be used by itself like that. the error is saying its an undefined function ha. and presuming that that worked, now how would i print out however many chunks of 150 or less there are? something like an array and then a while (($i=0;$i<count($chunkarray);$i++)){echo "".$chunkarray[$i]."";} Quote Link to comment https://forums.phpfreaks.com/topic/211920-simple-math-equation-help/#findComment-1104545 Share on other sites More sharing options...
DavidAM Posted August 28, 2010 Share Posted August 28, 2010 oops, sorry. I keep forgetting that int is not a function in php. It should be intval() $chunks = intval($OrderWeight / 150); $leftover = $OrderWeight % 150; This gives you a count of the number of times your multiplier is in the total and a leftover. To output it, you would print the multiplier $chunks times and then print the leftover: $mult = 150; // Our multiple for breaking down the value $total = 340; // The value we are starting with $chunks = intval($total / $mult); // How many multiples are in the total $left = $total % $mult; // What is left over (modulus operator) for ($i = 1; $i <= $chunks; $i++) { printf('..%d..', $mult); } if ($left > 0) printf('..%d..', $left); echo PHP_EOL; ?> OUTPUT: ..150....150....40.. If you need the values in an array for some reason, you can do something like this: $mult = 150; // Our multiple for breaking down the value $total = 340; // The value we are starting with $chunks = array(); // Start with an empty array $breakDown = $total; // Just so we don't wipe out our starting value while ($breakDown > 0) { if ($breakDown >= $mult) { $chunks[] = $mult; $breakDown -= $mult; } else { $chunks[] = $breakDown; $breakDown -= $breakDown; } } printf('%d contains the following: ', $total); foreach($chunks as $chunk) printf('..%d..', $chunk); print PHP_EOL;?> OUTPUT: 340 contains the following: ..150....150....40.. Quote Link to comment https://forums.phpfreaks.com/topic/211920-simple-math-equation-help/#findComment-1104612 Share on other sites More sharing options...
butsags Posted August 30, 2010 Author Share Posted August 30, 2010 Thanks man it worked perfectly. I didn't even know that such a thing as modulus exsisted (-__-) i need to go to college Hhaha, Thanks for your help my friend. Quote Link to comment https://forums.phpfreaks.com/topic/211920-simple-math-equation-help/#findComment-1105165 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.