PHP social contribution Yearly [closed]

39
November 24, 2021, at 2:30 PM
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 1 hour ago.

The community is reviewing whether to reopen this question as of 1 hour ago.

Improve this question

I'm working on a system report using PHP. I have a table with two columns (payment date and amount).

How I can use a while loop to calculate the total amount after each December through years? I tried to do it as per the screenshot attached but it displays the wrong sum after each December.

Below is how I did it:

 <?php 
    $total=0;
    $s = date('m');
    $c = mysqli_query($con,"SELECT * FROM payroll_contribution_pssf WHERE employee_id='3' ORDER BY contribution_id ASC");
    while($array = mysqli_fetch_array($c))
    {
        $m = date('m', strtotime($array['payroll_date']));
        echo $array['payroll_date']." ".$array['employee_amount']."<br>";
        if($m==12)
        {
        if($total == 0)
        $total = $array['employee_amount'];
        $total = $total + $array['employee_amount'];
        echo number_format($total,0)."<br>";  
        }
    }
    
    
    ?>

Answer 1

It seems you want it to calculate and display the total value of all the previous rows since the last total was calculated (or since the start of the dataset, in the case of the first iteration).

In that case the flaws in your logic are that you're

a) only adding to the total when it's the 12th month, and

b) you're not resetting it after you've reached the 12th month.

You need to have a "total" variable which is incremented each time you loop, and also you need to reset it to 0 after displaying it.

For example:

$total = 0;
$s = date('m');
$c = mysqli_query($con,"SELECT * FROM payroll_contribution_pssf WHERE employee_id='3' ORDER BY contribution_id ASC");
while ($array = mysqli_fetch_array($c))
{
    $m = date('m', strtotime($array['payroll_date']));
    echo $array['payroll_date']." ".$array['employee_amount']."<br>";
    $total += $array['employee_amount']; //increment every time
    if ($m == 12)
    {
        echo number_format($total, 0)."<br>";
        $total = 0; //reset after displaying
    }
}
READ ALSO
Couchbase APIs, libraries or curl commands to copy data from one cluster to another

Couchbase APIs, libraries or curl commands to copy data from one cluster to another

I want to copy my data from one cluster of Couchbase to another cluster either via curl commands or using SDK librariesCould someone please let me know if is there any libraries/APIs available by which I can be do this

54
Returning a array of objects by reducing and array

Returning a array of objects by reducing and array

I'm trying to return an array of objects from an arraySo for example

31
Android java foreground service

Android java foreground service

foreground service keeps particular activity in background or whole app , Like am making an app which gets values from sensorand fetch location

33
Linker Errors When Attempting to Run Example Program for MySQL Connector/C++ 8.0.27 (Windows)

Linker Errors When Attempting to Run Example Program for MySQL Connector/C++ 8.0.27 (Windows)

I have been trying to successfully build and run a test of the MySQL Connector/C++ 80

55