Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/211920-simple-math-equation-help/
Share on other sites

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]."";}

 

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..

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.