Calculating Size Dimensions in PHP - php

I'm trying to put together a simple calculation.
I have a size dimension of 100 x 100 x 100 which is a box.
I have individual items of 50 x 50 x 50.
If I keep adding items, I want to work out how many boxes is required to hold those items.
For example:
If I have 1 item of (50x50x50) then I will need 1 box to hold that item.
If I have 2 items of (50x50x50) then I will need 1 box to hold those 2 items.
If I have 3 items of (50x50x50) then I will need 2 boxes to hold those 3 items.
Any help would be greatly appreciated.

<?php
function GetItemsInBoxQuantity ($boxsize = array(100, 100,100), $verifiable_item = array(50,50,50)) { # here we pass default values of box size and verifable items
$boxspace = array_product($boxsize);
$verifiable_item_space = array_product($verifiable_item);
$count_boxes_in_boxspace = floor($boxspace / $verifiable_item_space);
return $count_boxes_in_boxspace;
}
# Now we putting new values of box size and size of items
$box = array(200,100,100);
$verifiable_item= array(40,40,40);
$result = GetItemsInBoxQuantity($box, $verifiable_item);
echo 'We can put into box '.$result.' items.';
?>
Output:
We can put into box 31 items.

Related

PHP and Phpmyadmin multiplication

I want to hit 2 values with the same id on PhpMyAdmin. According to the codes below, only the aggregation is running in the outputs I receive. I get a value of 0 when I multiply instead of adding.
<?php
$a = 0;
$oran = new sorgu;
$k_id = $kuponlar->veri->id;
$oran->select("*","matches","kid=$k_id");
while($oran->verioku()){
$a = $a+ floatval($oran->veri->oran);
}
echo $a;
?>
Screenshots:
It is necessary to multiply the "oran" values in there. But those with the same "kid" value are at least struck for that table.
That's the code that's running
Action Required:
1,25 x 1,5 x 1,3 x 1,55

Challenge: display php array with aggregated result

I am about to bleed with this challenge....anybody can help me with understanding how to solve please. I am missing the logic. I will spare you the details and give you the input variables to work with, which are dynamically generated. The below records are Male, Female, Male, Female....
$data=array(1,5,10,4,3,4,1,1,1,'2);
$indexOfSubTotalColumns=array(3,6,9,12,15,18,21);
//This means on the 3, 6th ...column of the HTML output, a summary is to happen. That is 1+5 will be 6 on column 3. 10 +4 will be 14 on column 6.
$groupTotalColumns=array(9,21);
//the 9th column will display sum of all columns before it. and 21 from col 10 on words, excluding the subtotals calculated or if you want.
With the following code,I have gotten to calculate the subtotals of each but can't put the dynamic grand total. First, add dumb data to the data array so it becomes equal to the number of HTML columns.
$previousValues=array();
foreach($data as $value){
if(in_array($i,$indexOfSubTotalColumns)){
$rows.="<td>".array_sum($previousValues)."</td>";
$rows.="<td>$value</td>";
$previousValues=array();
array_push($previousValues,$value);
$i++;
//
}
else{//not a subtotal
$rows.="<td>$value</td>";
array_push($previousValues,$value);
}
$i++;
//if $i reaches number of columns i.e. 21, close current row and open new
}
echo $rows;
The above code outputs me this layout:
I am failing to calculate the Totals.

While loop for TCPDF

I have problem to loop the data in TCPDF..The output should be display like this
Output :
Chair x 3
Table x 5
LCD X 2
but output now just show : Chair x 3
It only display the first record. Can anyone explain
how to loop the data based on output above?
$result_1=mysql_query($query_1);
while($row1=mysql_fetch_array($result_1)){
$item = $row1['item'];
$qty = $row1['SUM(qty)'];
$data = $item.' x '.$qty;
$tbl.='<td align="center">'.$data.'</td>';
$data='';
}
$tbl.='</tr>';
}
This picture output I get based on coding above
This is answer for my question. I just add $data. at $data = $item.' x '.$qty; and my problem has been solved.
$result_1=mysql_query($query_1);
while($row1=mysql_fetch_array($result_1)){
$item = $row1['item'];
$qty = $row1['SUM(qty)'];
$data = $data.$item.' x '.$qty.'<br>';
}
$tbl.='<td align="center">'.$data.'</td>';
$data='';
$tbl.='</tr>';
}

Golf league payout to six finishing positions

I have a golf league of 40 individuals. We all throw money in a pot and pay out the first 6 places based on final score.
If there were no ties the pay out would be simple but often we have, for example, 2 people tied for first place, 3 people tied for second, 1 person alone in third, etc. The variations seem endless.
I've been trying to automate the calculated payouts for each place using PHP but have not been successful. Any suggestions, help, or pointing in the right direction would be much appreciated. I noticed that someone else tried to ask a similar question on this site but was not successful in framing the question. I'll try to do a better job.
Here is some data I've been playing around with:
$playerNumber=40;
$totalPoints=100;
Payouts:
$pointsFirst=0.6*$totalPoints;
$pointsSecond=0.2*$totalPoints;
$pointsThird=0.15*$totalPoints;
$pointsFourth=0.03*$totalPoints;
$pointsFifth=0.02*$totalPoints;
$pointsSixth=0.01*$totalPoints;
For the example given above and to pay out six places, we would calculate the payouts as follows:
If two people are tied for first place, we add first and second place points and divide by two.
If three people are tied for second place, we add third, fourth and fifth place points and divide by three.
If one person is alone in third, this person would win sixth place points.
I can count the number of players who are in or tied for a certain place.
$countFirst=2;
$countSecond=3;
$countThird=1;
$countFourth=2;
$countFifth=1;
$countSixth=2;
In this example the player scores would be 72, 72, 73, 73, 73, 74, 75, 75, 76, 77, 77.
At first I thought this was an application for nested arrays. Then I thought perhaps using arrays, array slice, etc, may be a way to go. Each time I end up in the woods. I'm not seeing the logic.
I have used conditional statements for paying out three places but to pay out six places with this method puts me deep in the woods.
Example of payout to three places using conditional statements:
$pointsFirst=0.5*$totalPoints;
$pointsSecond=0.3*$totalPoints;
$pointsThird=0.2*$totalPoints;
if($countFirst>2) {
$ptsA=round($totalPoints/$countFirst,2);
}
elseif($countFirst==2) {
$ptsA=round(($pointsFirst+$pointsSecond)/2,2);
if($countSecond>1) {
$ptsB=round($pointsThird/$countSecond,2);
}
elseif($countSecond==1) {
$ptsB=round($pointsThird,2);
}
}
elseif($countFirst==1) {
$ptsA=round($pointsFirst,2);
if($countSecond>1) {
$ptsB=round(($pointsSecond+$pointsThird)/2,2);
}
elseif($countSecond==1) {
$ptsB=round($pointsSecond,2);
if($countThird>1) {
$ptsC=round($pointsThird/$countThird,2);
}
elseif($countThird==1) {
$ptsC=round($pointsThird,2);
}
}
}
I hope I have been clear in my request. I'll be glad to clarify anything. If anyone has any ideas on how to efficiently automate a payout calculation to six places I will be eternally grateful. Thank-you! Mike
Per request:
$scores=array();
$scores[0]=72;
$scores[1]=72;
$scores[2]=73;
$scores[3]=73;
$scores[4]=73;
$scores[5]=74;
$scores[6]=75;
$scores[7]=75;
$scores[8]=76;
$scores[9]=77;
$scores[10]=77;
$payout=array();
$payout[0]=0.6*$totalPoints;
$payout[1]=0.2*$totalPoints;
$payout[2]=0.15*$totalPoints;
$payout[3]=0.03*$totalPoints;
$payout[4]=0.02*$totalPoints;
$payout[5]=0.01*$totalPoints;
$countScores=array();
$countScores[0]=$countFirst;
$countScores[1]=$countSecond;
$countScores[2]=$countThird;
$countScores[3]=$countFourth;
$countScores[4]=$countFifth;
$countScores[5]=$countSixth;
First, there is a problem with your Payouts. If you add them up you get 1.01 not 1
0.6 (1st) + 0.2 (2nd ) + 0.15 (3rd) + 0.03 (4th) + 0.02 (5th) + 0.01 (6th) = 1.01
Second, it is easier if you make your Payouts and Counts into arrays -
change these -
$pointsFirst=0.6*$totalPoints;
$pointsSecond=0.2*$totalPoints;
$pointsThird=0.15*$totalPoints;
$pointsFourth=0.03*$totalPoints;
$pointsFifth=0.02*$totalPoints;
$pointsSixth=0.01*$totalPoints;
$countFirst=2;
$countSecond=3;
$countThird=1;
$countFourth=2;
$countFifth=1;
$countSixth=2;
to these
$payout=array();
$payout[0]=0.6*$totalPoints;
$payout[1]=0.2*$totalPoints;
$payout[2]=0.15*$totalPoints;
$payout[3]=0.03*$totalPoints;
$payout[4]=0.02*$totalPoints;
$payout[5]=0.01*$totalPoints;
$count=array();
$count[0]=2;
$count[1]=3;
$count[2]=1;
$count[3]=2;
$count[4]=1;
$count[5]=2;
Here is the start of one way to do it. Although I would eventually change this into a function so that I can use it again with different payouts, and number of places (see phpfiddle examples below)
I see this in 4 steps-
Step 1
// Add together the payments if there are ties - ie. 2 tied for first $payout[0]+$payout[1], etc
$payout_groups = array(); // start a payout array
$payout_groups_key = 0; // array key count
$payout_groups_count = 0; // array counter, use to match the $count array values
for($w=0;$w<count($payout);$w++){ //
if(array_key_exists($payout_groups_key,$payout_groups)){
$payout_groups[$payout_groups_key] += $payout[$w]; // if there are ties, add them together
}
else{
$payout_groups[$payout_groups_key] = $payout[$w]; // else set a new payout level
}
$payout_groups_count++; // increase the counter
if($payout_groups_count == $count[$payout_groups_key]){ // if we merged all the ties, set a new array key and restart the counter
$payout_groups_key++;
$payout_groups_count = 0;
}
}
Step 2
// basic counter to get how many placers/winners. This makes it possible to have ties for 6th (last) place
$x = 0;
$y = 0;
while($y < count($payout)){
$y += $count[$x]; // the $count array values until we reach the amount of places/payouts
$x++;
}
Step 3
// Create array for winnings per placing
$winnings = array(); // start an array
$placings_count = 0; //
$placings_counter = 0;
for($z=0;$z<$y;$z++){
$winnings[$z] = $payout_groups[$placings_count]/$count[$placings_count];
$placings_counter++;
if($placings_counter == $count[$placings_count]){
$placings_count++;
$placings_counter = 0;
}
}
Step 4
// Assign winnings to scorecard
$scoreboard = array();
for($t=0;$t<count($winnings);$t++){
$scoreboard[$t]['score'] = $scores[$t];
$scoreboard[$t]['payout'] = $winnings[$t];
}
You can see this using your defined values at - http://phpfiddle.org/main/code/a1g-qu0
Using the same code above, I changed the payout amounts, and increased it to 7th places - http://phpfiddle.org/main/code/uxi-qgt

Add all instance of a variable together in a PHP foreach loop?

I am trying to do something that's quite complex (for me anyway) and I've got a point which has stopped me in my tracks.
In this project, you enter the weight of a box, and the percentage of materials held in that box. So for example, a box may have 10% of wood, 40% of plastic, and 50% of paper. The weights of the materials are held in the database as they dont change. So the value of 1% of wood is held in the database, as 1% of plastic and paper.
The controller should then take the percentage of material given, and then multiply it by the 1% value held in the database.
So far, this is my code:
foreach($wastestream as $ws) {
$genweight = $ws['Materialtype']['1pc'] * $ws['Valuegiven']['percent'];
}
And this gives the following values:
10% Wood = 97.5
40% Plastic = 100.4
50% Paper = 40.3
The problem I now have is that I have to add all of these variables together. So essentially what I want to do after it pumps out the values is to add them all up together, so:
97.5 + 100.4 + 40.3 = 238.2
What should I do?
Cheers!
I think I have misunderstood something, since it seems not complicated at all...
$total=0;
foreach($wastestream as $ws) {
$total += $ws['Materialtype']['1pc'] * $ws['Valuegiven']['percent'];
}
$total will hold the total value
Could you add the values as they are created?
$totalweight = 0;
foreach($wastestream as $ws) {
$genweight = $ws['Materialtype']['1pc'] * $ws['Valuegiven']['percent'];
$totalweight += $genweight;
}

Categories