PHP social contribution Yearly - php

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>";
}
}
?>

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;
$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
}
}

Related

Comparing values from a database using a while loop

I have a MySql table where I saved all workers names and the dates workers have to work on. I want to show a list containg all days of the current month and the worker names who have to work on the day that corresponds to them. Example:
February
1
2
3 - John Wick
5
6 - Martha Beck
etc.
This is the code I have in PHP but the loop is not working. I just get a list from 1 to 30 but it is not showing the data from database. If I run the loop without the (while ($n < 31)), I get all the records from database but I want to show the names just beside the day that correspond.
<?php
mysql_select_db($database_nineras, $nineras);
$query_res = sprintf("SELECT res_id, res_dateini, res_datefin, res_name FROM reservas ORDER BY res_dateini DESC");
$reservas = mysql_query($query_res, $nineras) or die(mysql_error());
$rreser = mysql_fetch_assoc($reservas);
$treser = mysql_num_rows($reservas);
$n = 1;
while ($n < 31) {
do {
++$n;
if ($n == date('d', strtotime($rreser['res_dateini']))) {
echo $n . ' - ' . $rreser['res_name'];
}
else {
echo $n;
}
} while ($rreser = mysql_fetch_assoc($reservas));
}
?>
The problem with your code is that the do-while loop is fetching all the rows returned by the query. So when you get to the second iteration of the while loop there's nothing left to fetch.
Rather than fetch the rows from the database each time through the loop, you can fetch them once and put them into an array whose index is the day numbers. Then you can loop through the days and print all the rows for each day.
Use date('j', ...) to get the date without a leading zero. Or change your SQL query to return DAY(res_dateini).
$results = array();
$reservas = mysql_query($query_res, $nineras) or die(mysql_error());
while ($rreser = mysql_fetch_assoc($reservas)) {
$d = date('j', strtotime($rreser['res_dateini'])));
$results[$d][] = $rreser['res_name'];
}
for ($day = 1; $day <= 31; $day++) {
echo "$day - " . (isset($results[$day]) ? implode(", ", $results[$day]) : "") . "<br>\n";
}
DEMO

Date Comparison to update records

I'm getting records from a MySQL database with this PHP function:
function someFunction($date){
// all distinct records
$query = "select count(distinct column_name) as alias from table_name where DATE(date_column) = '$date'";
$result = $connection->query($query);
$row = $result->fetch_assoc();
return $row['alias'];
// end of all distinct records
}
Now what the below PHP code does is, get the day in the date, compute the week of the month it belongs to and stores it an an array.
//while fetch_assoc returns records
//$result1 query: "select * from table_name where DATE(date) between '$first_date' and date_add('$end_date',interval 1 day)"
while ($row1 = $result1->fetch_assoc()) {
$date = $row1['date'];
$start = 1;
$end = 7;
for ($i = 1; $i <= 5; $i++) {
if ((int) date('d', strtotime($date)) >= $start && (int) date('d', strtotime($date)) <= $end) {
if (!isset($arr1[$i]) || !isset($arr2[$i])) {
$arr1[$i] = 0;
$arr2[$i] = 0;
}
++$arr1[$i];
$arr2[$i] = someFunction(date('Y-m-d', strtotime($date)));
}
$start += 7;
$end += 7;
}
}
Consider 1st, 2nd and 3rd belong to the same week, 1st has 3 records, 2nd has 4 and 3rd has 1. The while loop will iterate 7 times, each value returned by the someFunction() overwriting the value in $arr2[$i].
So my question is, how will I be able to check if the previous iteration date value is equal to the current date value?
So my question is, how will I be able to check if the previous iteration date value is equal to the current date value?
Pseudocode:
$lastValue = …; // initialization with a value that does not occur in the actual values,
// such as NULL, empty string, …
while(…) {
if($currentValue == $lastValue) {
// do something
}
else {
// do something else
}
// …
$lastValue = $currentValue; // set current value for next loop interation
}

How can I generate a random number every x amount of months?

Starting with the number 9 and using php, I would like to be able to count up from there, and echo out the next number in increments of 1.
So, number 9, then after 1 month the number would change to 10, then another month 11, then 12 etc., with no maximum number/stop point.
How can I accomplish this? So far I have the below code.
$number = 9;
$output = $number + 1;
echo $output;
Is there a way to set this to increase once a month?
You can do this with the PHP date()-function. This is one example of doing it if you are not dependent on the day of the month, but adding day functionality is possible and should be quit easy.
$startNumber = 9;
$startYear = 2015;
$startMonth = 9;
$currentYear = intval( date( "Y" ) );
$currentMonth = intval( date( "n" ) );
$monthsToAdd = ( ( $currentYear - $startYear ) * 12 )
+ ( $currentMonth - $startMonth );
echo $startNumber + $monthsToAdd;
From your question, I'd say:
$number = 9;
$output = date('n') + $number;
echo $output;
But that depends on what you are trying to accomplish. You can also wrap the number around the date() with a modulo.
However this is nothing random. If you want to create a random number every month like your topic suggests, use the month as the random seed.
srand(date('n'));
$number = rand();
a very inefficient way would be
<?php
function increm($duration){
while ($i<$duration) {
$i++;
}
return true;
}
$number = 9;
$start = time();
$i = 0;
while (1){
increm(3600*24*30);
$i++;
// Do your code
}
?>
this script would have to be run continuously for months.
A better way would be
<?php
$number = 9;
if(!file_exists('date.txt')){
$date=date('n');
file_put_contents( (string)time());
$date = 0;
}
else{
$date= file_get_contents('date.txt');
$date= date()-(int)$date;
$date= floor($date/(24*3600*30));
}
// do whatever you may
?>
But this script would increase it whenever called as the first open date would be stored. Will work forever (till UNIX can timestamp).
for this purpose you have to store the number in the database, compare with current unix timestamp and update it when the new month is reached.
2 database columns: count_month int(10) and next_month int(10) where next_month will contain the unix timestamp of the first day of the next month. you can run it with cronjobs or on production.
<?php
$now = strtotime("now");
$next_month = strtotime("first day of next month");
if ($query = $dbconnect->prepare("SELECT next_month FROM table1")) {
$query->execute();
$query->bind_result($compare_time);
$query->store_result();
$row_count = $query->num_rows;
if ($row_count > 0) {
while ($query->fetch()) {
if ($compare_time < $now) { // you reached the 1th of the next month time to update
if ($query2 = $dbconnect->prepare("UPDATE table1 SET count_month=count_month +1, next_month=?")) {
$query2->bind_param('i', $next_month);
$query2->execute();
$query2->close();
}
}
}
}
$query->free_result();
$query->close();
}
?>

How to calculate Due Fees?

I want to calculate due fees, w.r.t current month. All session of schools starts from Apr, and fees starts from Apr.
If student not pay any fees in Apr, May, Jun, and now paying fees in Aug. Than Due fees must be Apr + May + Jun.
But I`m confused how to get due fees based on current month.
My code:
$current_month = date('M');
$final = 0;
for($i='Apr'; $i<$current_month; $i++)
{
$query_run = mysqli_query($conn,"SELECT amount FROM fees_on_class WHERE class = '$class'");
while ($num = mysqli_fetch_assoc ($query_run))
{
$temp += $num['amount'];
}
$final = $final + $temp;
}
mysqli_query($conn,"INSERT into dues(registration_number, due_fees)VALUES('$registration_number','$final')");
Code is not working and giving Zero (0).
Thanks.
Alphabetic value in for loop you are using will not work as you want.
for($i='Apr'; $i<$current_month; $i++)
{
}
Please make month loop numeric.
for($i='4'; $i<$current_month; $i++)
{
}
I have not checked the whole code, but this is the first error I can see..

php - get sum of clicks for past 7 days

I have a table xeon_users_rented, with: clicks0, clicks1, clicks2, clicks3, clicks4, clicks5, clicks6
Each day, clicks0 will increase, and every day at midnight, a cronjob will run, making clicks0 = clicks1 (setting todays clicks, to yesterday clicks), and then set clicks0 to zero.
What I am trying to achieve is I want to make a graph, that shows the sum of clicks0, clicks1 etc., where clicks0 is todays date.
I have the query below:
$data = array();
for ($x = 0; $x <= 6; $x++) {
$date = date("Y/m/d", time() - ($x * 86400));
$queryE = $dbh->prepare("SELECT SUM(clicks$x) FROM xeon_users_rented WHERE user_by=:username");
$queryE->bindParam(":username", $userdata['username']);
$queryE->execute();
$row = $queryE->fetch(PDO::FETCH_ASSOC);
$dates[] = date("Y/m/d", time() - ($x * 86400));
$data[] = ($row['clicks'.$x.''] > 0 ? $row['clicks'.$x.''] : 0);
}
$days = array('Today');
for ($i = 0; $i < 6; $i++) {
$days[$i] = date('d-m', strtotime('-'.($i + 0).' day'));
}
The $days is working perfectly - it will print out today, and the last couple of days.
The $data is not working. It is just printing out:
0,0,0,0,0,0,0
Can someone please help me out here.
The column from your SUM isn't going to be named clicks$x. It will be named something like SUM(clicks1).
Provide an explicit name in the SQL, like
SELECT SUM(clicks$x) as clickSum ...
Then reference it in row as
$row['clickSum']

Categories