I am coding inventory of products. Now I am in the process of computing or converting the items into a larger unit.
For example:
Product A: 1 box = 12 bottles
The user can input a data like 3 boxes and 13 bottles when adding a transaction. And the new value for Product A will be 3 boxes and 13 bottles in storage. Data will save into database tbl_transaction.
How can I automatically turn/convert the items as a whole like 4 boxes and 1 bottle in storage to add in my tbl_storage?
I have tried this formula but I am afraid it is not accurate when the number of bottles is in the decimal point.
$bottles = 13;
$box = 12;
$remaining_in_bottle = number_format($bottles / $box);// this will only convert the bottle into box (also tried float but not sure what I am doing)
$total_box = 3 + ???;
echo $total_box." boxes and ".$remaining_in_bottle ." bottles in storage
I am assuming the user is inputing just digits as values for boxes and bottles, but if not you will simply need to extract these values from the string before performing the following calculations:
Code: (Demo)
$bottles_per_box=12;
$user_input_bottles=13;
$user_input_boxes=3;
if($user_input_bottles>$bottles_per_box){
// add truncated integer to box count. DO NOT USE ROUND(), USE FLOOR()
$user_input_boxes+=floor($user_input_bottles/$bottles_per_box);
// store remainder after division
$user_input_bottles=$user_input_bottles%$bottles_per_box;
// ^-- modulo operator
}
echo "New Bottles Total: $user_input_bottles\n";
echo "New Boxes Total: $user_input_boxes\n";
Output:
New Bottles Total: 1
New Boxes Total: 4
I assume that you want to input different for tbl_transaction and tbl_storage.
CODE
//Max bottle per box
$box_max_bottles = 12;
//User Input
$input_box = 3;
$input_bottle = 13;
//Transaction
$transaction = (($input_box > 1) ? $input_box . ' boxes' : $input_box . ' box')
. ' and ' . (($input_bottle > 1) ? $input_bottle. ' bottles' : $input_bottle. ' bottle'). ' in storage';
//Data will save into database tbl_transaction
echo $transaction;
//Get the remainder which is the remaining bottle
$total_bottle = $input_bottle % 12;
//Get the total boxes and divide the bottle into 12
$total_box = floor($input_box + ($input_bottle / 12));
echo "<br />";
//Storage
$storage = (($total_box > 1) ? $total_box . ' boxes' : $total_box . ' box')
. ' and ' . (($total_bottle > 1) ? $total_bottle . ' bottles' : $total_bottle . ' bottle'). ' in storage';
//Data will save into database tbl_storage
echo $storage;
OUTPUT
Transaction
3 boxes and 13 bottles in storage
Storage
4 boxes and 1 bottle in storage
Related
I have a form, when a user fills in the total number of bottles they have, it inserts into a database and then should sum up how many cases there are.
For example in wine - there are 12 bottle cases, if a user puts in 100 bottles, it should divide this by 12 and give the sum of 8.33333333.
$bottles = "100";
What is the best way to round this down to just the number 8 and then work out how many bottles are left that never made it into a full case?
Hope this makes sense.
You can use floor
$bottles = "100";
$case = floor( $bottles / 12 );
echo $case;
Will result to 8
Documentation: http://php.net/manual/en/function.floor.php
If you want to check the bottles left, you can use modulo
$bottles = "100";
$left = $bottles % 12;
Will result to 4
You can use floor to round down, and the modulo (%) operator to determine how many bottles are left.
$bottles = 100;
$bottles_per_case = 12;
print "There are " . floor($bottles / $bottles_per_case) . " cases...";
print "With " . ($bottles % $bottles_per_case) . " bottles left over";
$bottles = "100";
$case = (int) $bottles / 12 ;
echo $case;
$left = $bottles % 12;
echo '<br>left: ' . $left;
how can I multiply the result of an array_sum in a foreach loop?
Example: I have product1 with a value of 10, product 2 with a value of 20. Each product is bought twice: I need to have (10 + 20)*2 = 60. I tried the following:
($number[$key]) * (array_sum($totprod[$key]))
but this is not working (result = blank).
Thank you for your help
Here is my foreach ($number = quantity)
foreach ($number as $key => $value){
$total[$key] = array($product1[$key], $product2[$key]);
echo "<pre>Quantity " . $number[$key] . "Price product1 " . $product1[$key] . " Price product2 " . $product2[$key] . " = Total price " . $number[$key] * array_sum($total[$key]) . "</pre>";
}
I don't think that sum is the best approach here since
(10 + 20) * 2 = 2*10 + 2*20
You probably better just calculate totals for each products and sum all results as it would work in the general case (1 productX + 6productY).
Now generally speaking, dealing with money, you should avoid array_sum and all php native math operators as you could have some surprises like total VAT != sum of all product's VAT.
When dealing with money, or when precision must be controlled, use bcmath http://php.net/manual/en/book.bc.php
I'm counting the number of elements in a section and it's an even number like 34, I would like to split them into three logical groups, like:
Group 1: 1 to 12 (12 items)
Group 2: 13-24 (12 items)
Group 3: 25-34 (10 items)
I want the PHP code to logically create three groups of items where the first two sets can hold equal number of items and the rest can go into the last set. There won't be more than three sets.
$whole_elements=33;
$group1 = ($whole_elements) / 3;
This kind of code would not work as it would return a value with decimal point.
You should divide your number by 3, round the result to obtain the first 2 numbers, then use a modulo to get the last one:
<?php
$whole_elements=34;
$group1 = $group2 = round($whole_elements / 3);
$group3 = $whole_elements % $group1;
?>
Then obtaining the "groups" is a matter of slicing the original array:
<?php
$group1_array = array_slice($original_array, 0, $group1);
$group2_array = array_slice($original_array, $group1, $group1);
$group3_array = array_slice($original_array, -$group3);
?>
In fact, using this method, you don't need to determine $group2 in the first bit of code as it's the same as $group1
$whole_elements = 34;
$third = ceil($whole_elements / 3);
$group1 = $group2 = $third;
$group3 = $whole_elements - 2*$third;
echo 'one: ' . $group1 . '<br/>two: ' . $group2 . '<br/>three: ' . $group3;
one: 12
two: 12
three: 10
'Ello
I'm trying to get the items from a client populated "order form" to ONLY list the items that the client has designated a quantity value of 1 or more to be sent as a Purchase Order receipt that is generated and sent via email once they hit Submit.
Here's what I have so far on that part:
$i = 1;
$imax = 4;
echo "Products<br />";
echo "-------------------------------------------------------------<br />";
while ($i <= $imax) {
$itemqty = ${'qty'.$i};
$itempn = ${'pn'.$i};
$itemdesc = ${'desc'.$i};
$itemprice = ${'value'.$i};
$itemtotalprice = ${'elinetotal'.$i};
if ($itemqty !== 0) {
echo $itemqty . " x " . $itemdesc . " (" . $itempn . ") # $" . $itemprice . " ea. = $" . $itemtotalprice . "<br />";
}
$i++;
}
It lists everything correctly, except it doesn't disregard the items with a value of 0. It'll list them like this:
Products
-------------------------------------------------------------
0 x Item #1 Description (HOSE-12) # $155.00 ea. = $0.00
5 x Item #2 Description (GAUGE-2) # $51.00 ea. = $255.00
0 x Item #3 Description (PTC) # $0.70 ea. = $0.00
10 x Item #4 Description (PT-234R) # $15.94 ea. = $159.40
It may be the easiest fix, but can anyone shed some light on this? I'd greatly appreciate it!
My initial guess is $itemqty is the string '0'. Try using the not equal operator, eg
if ($itemqty != 0)
instead of the not identical operator, ie !==
Otherwise, you could try casting the $itemqty assignment as an integer, eg
$itemqty = (int) ${'qty'.$i};
I am working on website that suppose to compare products. So I have reached to this following array
Array ( [iPhone 4 8GB Black] => 319 [iPhone 4S] => 449 [iphone 5] => 529 )
the key of the array is product name and the value of the array is the price. now i want to translate this array into statements like
iphone 4 8GB Black is cheapest!
iPhone 48GB Black is £130(calculation:449-319) cheaper than iphone 4S.
iPhone 48GB Black is £210(calculation:529-319) cheaper than iphone 5.
iPhone 4S is £80(calculation:529-449) cheaper than iphone 5.
iphone 5 is most expensive product from your chosen list.
Please help me on how to output those statements from an array. Your suggestion to do something else with this array in-terms of comparing would also be great. Thank you.
First, you have to sort your array with asort (in order to keep the association between your index and your values, and sort on values).
asort($yourArray);
Then, as your array is sorted, you can isolate price and names.
$names = array_keys($yourArray);
$prices = array_values($yourArray);
At this point you have 2 numerical indexed array containing your label and your prices and these 2 arrays are synchronized.
Finally, you just have to loop from 0 to the length of your array (one of them, its the same size) and made your process:
for($i = 0 ; $i < count($names) ; $i++)
{
if ($i == 0)
{
// First product -> cheapest
echo "The product " . $names[$i] . " is cheapest";
}
else if ($i == (count($names) - 1))
{
// Last product, the most expensive
echo "The product " . $names[$i] . " is the most expensive product of the list";
}
else
{
// calculate the diff between current product and first product
$diff = $price[$i] - $price[0];
echo "The product " . $names[$i] . " is " . $diff . " more expensive than " . $names[0];
}
}
This example make all comparision to the first product.
If you need all combination, it is a little more complexe, you have to make a double loop:
// Hard print the first product
echo "The product " . $names[0] . " is the cheapest";
// Make all possible comparisions
for($j = 0 ; $j < (count($names) - 1) ; $j++)
{
for($i = ($j+1) ; $i < count($names) ; $i++)
{
// calculate the diff between current product and first product
$diff = $price[$i] - $price[$j];
echo "The product " . $names[$i] . " is " . $diff . " more expensive than " . $names[$j];
}
}
// Hard print the last product
echo "The product " . $name[count($names) - 1] . " is the more expensive";