Challenge: display php array with aggregated result - php

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.

Related

Calculating Size Dimensions in 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.

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

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

PHP: "While" loop returns same value for each iteration of the variable

Thanks in advance for any help offered.
I'm performing a query that returns something like this:
route | busnumber
2 300
4 123
2 455
12 934
My goal is to print/echo a list of all "busnumber" were "route" = 2.
I know I can do a query on the DB to do this but I don't want to run this query for each and every route. The result set is actually part of one master, complex query so I need to accomplish my goal using the array.
The query works correctly and does display the appropriate info. I also return the results into an array. i.e ...
$query=("SELECT * from foo")
$result=mysql_fetch_assoc($query);
My current code looks like this (note my comments):
mysql_data_seek( $result,0); // because I'm previously iterating through $result
while ($i2=mysql_fetch_assoc($result)) {
$busnumber=$i2['busnumber'];
$busroute=$i2['route'];
echo "<div class='businfo'>";
if ($busroute='2'){ // I only want this to happen if $busroute=2
echo "Current Route = $busroute</br>";
echo "Bus Number : $busnumber </br>";
}
echo "</div>";
}
What I'm getting, however, it is echoing the same '$busroute' (2) but different '$busnumber' for each row as such:
Current Route = 2
Bus Number : 194
Current Route = 2
Bus Number : 196
Current Route = 2
Bus Number : 2002
Current Route = 2
Bus Number : 2010
Current Route = 2
Bus Number : 2013
The problem is that all of those bus numbers are not part of route 2. It is a listing of bus numbers from all routes. I only want it to perform the foreach loop if $routenumber=2.
Thanks again for the help :)
if ($busroute='2'){
The problem is that you're not checking whether it's equal to 2 here, you're assigning it to equal 2. Change it to this and you'll be fine:
if ($busroute=='2'){
$query = "SELECT * from foo";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
if ($row['route'] == 2) {
// do your stuff
}
}

How to retrieve 1 record at a time from db

I have a table comment as follow:
Comment
comment_id cmt followupid
1 Hello 3
2 hi 4
3 Hey 2
4 wassup 1
My query is that I want to echo "Hello", "hi", "hey" , "Wassup" and other (the record continues) individualy, I have used
$comment = mysql_result($runQuery, $i,"cmt");
echo $comment;
which works fine but the problem is that it echoes all the comments at once, what I want is to echo all the comment but one at a time. the comment are in a div tag such that each the div appears only after 1 second the page is loaded. I want each comment to appear after different time interval
for e.g:
Hello to appear at 5pm (not necessarily the corect time it can be just an int)
hi 5.10 pm
hey 6.30 pm
Please Help!
The following code should give you some hints.
$result = mysql_query($runquery);
while($row=mysql_fetch_assoc($result)){
// $row contains a single row.
echo $row['cmt'], $row['comment_id']
}
Create another variable storing time divisions(or number of rows). So that different output at different time can be fetched. For eg. If your table has 24 rows(or items), then this variable shall have a value 24. Use it to divide you output times(As in 24 hours, each hour a different value).
Now, the PHP part(I am not much familiar with date and time functions in PHP, so you can totally ignore the paragraph above.
$result = mysql_query($runquery);
$j = 0;
$i = rand( 0, mysql_num_rows($result) );
while($row=mysql_fetch_assoc($result)){
// $row contains a single row.
if( $j++ = $i )
echo $row['cmt'], $row['comment_id'];
}
This will fetch one random row from the table, but not depending upon the server time.

Categories