I want to show Total Salary, which is the sum of all employee's net salary. How can I do that? I have tried some code but its not working
<tbody>
<?php
$salary = 0;
if (!empty($emp_salary_info)):foreach ($emp_salary_info as $v_emp_salary):
$salary = $salary + $v_emp_salary->basic_salary;
?>
<tr>
<td><?php echo $v_emp_salary->employment_id; ?></td>
<td><?php echo $v_emp_salary->first_name . ' ' . $v_emp_salary->last_name ?></td>
<td><?php echo $gross = $v_emp_salary->basic_salary + $v_emp_salary->house_rent_allowance + $v_emp_salary->medical_allowance + $v_emp_salary->special_allowance + $v_emp_salary->fuel_allowance + $v_emp_salary->phone_bill_allowance + $v_emp_salary->other_allowance ?></td>
<td><?php echo $deduction = $v_emp_salary->tax_deduction + $v_emp_salary->provident_fund + $v_emp_salary->other_deduction ?></td>
<?php $net_salary = $gross - $deduction ?>
<td><?php echo $net_salary ?></td>
<td><?php
if ($v_emp_salary->employment_type == 1) {
echo 'Provision';
} else {
echo 'Permanent';
}
?></td>
<td><?php echo btn_view('admin/payroll/view_salary_details/' . $v_emp_salary->employee_id); ?></td>
<td>
<?php echo btn_edit('admin/payroll/manage_salary_details/' . $v_emp_salary->employee_id . '/' . $v_emp_salary->designations_id); ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
this is code I have tried for sum the net salary
<?php
$salary = 0;
if (!empty($emp_salary_info)):foreach ($emp_salary_info as $v_emp_salary):
$salary = $salary + $v_emp_salary->basic_salary;
echo 'Total Salary :'
?>
<?php echo $salary; ?>
I think you are using data-tables for displaying table records. Try to print Total after completing record calculations.
In image you are displaying total before completing process.
Also change few lines of code
if(!isset($v_emp_salary->basic_salary) || empty($v_emp_salary->basic_salary) || !is_numeric($v_emp_salary->basic_salary))
{
$v_emp_salary->basic_salary = 0;
}
$salary = $salary + $v_emp_salary->basic_salary;
Still if you do not find total try to check data inside $emp_salary_info it may have empty or wrong data with salary.
Related
I have this working.
$queryPR = "SELECT * FROM `raport` WHERE DATE(`timestamp`) = CURDATE() AND (lokacioni = 'PR')";
I need the total amount of rows calculated for specific date.
I need also to the SUM('pagesat') somehow but I have no idea how.
Find the whole code below.
I am trying to build something simple to show the income and outcome for 3 locations that we have.
<table class="responsive-table">
<thead>
<tr>
<th>Vetura</th>
<th>Targa</th>
<th>Dite</th>
<th>Pagesa</th>
<th>Cmimi Ditore</th>
<th>Vazhdim</th>
<th>Te Tjera</th>
<th>Shpenzime</th>
<th>Komente</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM `raport` WHERE DATE(`timestamp`) = CURDATE() AND (lokacioni = 'FR')";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php echo $row['vetura']; ?></td>
<td><?php echo $row['targa']; ?></td>
<td><?php echo $row['dite']; ?></td>
<td><?php echo $row['pagesa']; ?>€</td>
<td><?php echo $row['cmimiditore']; ?>€</td>
<td><?php echo $row['vazhdim']; ?>€</td>
<td><?php echo $row['tetjera']; ?>€</td>
<td><?php echo $row['shpenzime']; ?>€</td>
<td><?php echo $row['koment']; ?></td>
</tr>
<?php
$pagesa = $row['pagesa'];
$tetjera = $row['tetjera'];
$vazhdime = $row['vazhdim'];
$shpenzime = $row['shpenzime'];
}?>
</tbody>
</table>
<?php
$totali = ($pagesa + $vazhdime + $tetjera);
$gtotal = ($totali - $shpenzime);
?>
<div class="card grey">
<hgroup class="totalDitorRaport">
<h2>Total</h2>
<h1><?php echo $gtotal; ?>€</h1>
</hgroup>
</div>
try this
get the sum of the row pagesa in the loop
$queryPR = "SELECT * FROM `raport` WHERE DATE(`timestamp`) = CURDATE() AND (lokacioni = 'PR')";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
$sum = 0;
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
$sum += $row['pagesa'];
}
echo $sum;
I'm kind of new in php and i'm searching a way to get some words from a specific column that i get from a sql server stored proc. These 'words' will me to use them as style for my css
Example :
Column HTML_CODE has sometime 'bold', 'green' or something else. Sometime they are in the same column, not always in the same order. I don't know how to use them in my loop. See what i has until now :
$result = sqlsrv_query($conn, $sql);
while ($row = sqlsrv_fetch_array($result))
{
//print_r( $row ); // debug code
if ($row['HTML_CODE'] == 'BOLD(), Green()'){
$couleur='green';
$font= 'bold';
}
else {
$couleur='black';
$font= 'normal';
}
?>
<tbody>
<tr>
<?php echo "<tr style=\"font-weight:$font; color:$couleur;\">"; ?>
<td><?php echo ($row['Nom']); ?></td>
<td><?php echo ($row['Quantite']); ?></td>
<td><?php echo ($row['Montant']); if (is_numeric($row['Montant'])) {
echo ' $';
}?></td>
<td><?php echo ($row['#GL']); ?></td>
<td><?php echo ($row['Debit']); if (is_numeric($row['Debit'])) {
echo ' $';
} ?></td>
<td><?php echo ($row['Credit']); if (is_numeric($row['Credit'])) {
echo ' $';
}?></td>
</tr>
</tbody>
if( strstr($row['HTML_CODE'], "BOLD()")){ $font= 'bold'; }
I have created a simple Business Management setup for office work. First I fetch all information from database. All is going well except the quantity input.
In this all payment doing calculate sum and do Echo. But I want that this echo value detect automatic store in database in another table.
so how to complete this?
This is the code of program.
<tbody>
<?php
$respectivestud = mysql_query("select * from client where client_id");
$i = 1;
while($r_client = mysql_fetch_array($respectivestud))
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $r_client['name']; ?></td>
<td><?php echo $r_client['project_name']; ?></td>
<td><?php echo $r_client['cost']; ?></td>
<td><?php echo $r_client['payment_1']; ?></td>
<td><?php echo $r_client['payment_2']; ?></td>
<td><?php echo $r_client['payment_3']; ?></td>
<td><?php echo $r_client['payment_4']; ?></td>
<td><?php echo $r_client['payment_5']; ?></td>
<?php $total = $r_client['payment_1'] + $r_client['payment_2'] + $r_client['payment_3'] + $r_client['payment_4'] + $r_client['payment_5'];?>
<td> <?php echo $total ; ?></td>
<?php $remain = $r_client['cost'] - $total;?>
<td><?php echo $remain; ?></td>
**<td>**
Please make sure you write valid PHP code. You are not closing your while and you are not closing tags correctly. This should be correct.
<tbody>
<?php
$respectivestud = mysql_query("select * from client where client_id");
$i = 1;
while($r_client = mysql_fetch_array($respectivestud))
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $r_client['name']; ?></td>
<td><?php echo $r_client['project_name']; ?></td>
<td><?php echo $r_client['cost']; ?></td>
<td><?php echo $r_client['payment_1']; ?></td>
<td><?php echo $r_client['payment_2']; ?></td>
<td><?php echo $r_client['payment_3']; ?></td>
<td><?php echo $r_client['payment_4']; ?></td>
<td><?php echo $r_client['payment_5']; ?></td>
<?php $total = $r_client['payment_1'] + $r_client['payment_2'] + $r_client['payment_3'] + $r_client['payment_4'] + $r_client['payment_5'];?>
<td> <?php echo $total ; ?></td>
<?php $remain = $r_client['cost'] - $total;?>
<td><?php echo $remain; ?></td>
</tr>
<?php
}
?>
</tbody>
To save data you could use this:
$sql=mysqli_query($connection,"insert into `tablename`(`columnname`)values('".$total."')");
foirst off all replace mysql_* with mysqli_* or Pdo statement. as mysql_* is depricated now . here is the reference mysqli and pdo
store the remaining ammount in a variable. then use this variable in update query . like
<td><?php echo $remaining_amount=$remain; ?></td>
then update in db
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE MyGuests SET tablename='".$remaining_amount."' WHERE id=yprimarykey";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
<td><?php echo $r_client['payment_5']; ?></td>
<?php $total = $r_client['payment_1'] + $r_client['payment_2'] +$r_client['payment_3'] + $r_client['payment_4'] + $r_client['payment_5'];?>
<td> $sql=mysqli_query($connection,"insert into `tablename`(`columnname`)values('".$total."')");</td>
you can do like this whatever the value you want to insert...
I want to recapitulate report that grouped by month. But the results of sum is incorrect. It's just sum data in one month and show in all rows.
<?php
$query = mysql_query("SELECT * FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php
$Masuk= mysql_query("SELECT SUM(barang_in) AS masuk FROM barang GROUP BY arsip");
if($Masuk){
$data = mysql_fetch_assoc($Masuk);
echo $data['masuk'];
}
?></td>
<td><?php
$keluar= mysql_query("SELECT SUM(bijih_out+htm_out+pth_out) AS keluar FROM barang GROUP BY arsip");
if($keluar){
$data = mysql_fetch_assoc($keluar);
echo $data['keluar'];
}?></td>
<td><?php
$efisiensi= mysql_query("SELECT SUM((bijih_out+htm_out+pth_out)-barang_in) AS efisiensi FROM barang GROUP BY arsip");
if($efisiensi){
$data = mysql_fetch_assoc($efisiensi);
echo $data['efisiensi'];
}
?></td>
<td><?php
$persen= mysql_query("SELECT SUM(barang_in/(bijih_out+htm_out+pth_out))*1 AS persen FROM barang GROUP BY arsip");
$simbol = "%";
if($persen){
$data = mysql_fetch_assoc($persen);
echo number_format($data['persen'],2); echo $simbol;
}
?></td>
<?php
$i++;
}
?>
so that's my code. what's wrong with that? There are some columns for displaying income items, outcome items, efficiency ( difference of outcome and income items ) and percent of efficiency.
finally I found out the right code. Thankyou for #RandomSeed who helps me. This is my final code and it works!
<?php
$query = mysql_query("SELECT arsip, SUM(barang_in) AS income, SUM(bijih_out+htm_out+pth_out) AS outcome, SUM((bijih_out+htm_out+pth_out
)-barang_in) AS efficiency, SUM((barang_in / ( bijih_out + htm_out + pth_out))*100) AS percent FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php echo $data['income']; ?></td>
<td><?php echo $data['outcome']; ?></td>
<td><?php echo $data['efficiency']; ?></td>
<td><?php $simbol = "%"; echo number_format($data['percent'],0); echo $simbol; ?></td>
<?php
$i++;
}
?>
<?php foreach((array)$query as $row):?>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
<?php endforeach ;?>
I want to sum all values in $row->cost when $row->date=02-01-2016, like this in a table
Thanks.
echo array_sum(array_filter(array_map(function($row){
if($row->date == '02-01-2016')
return $row->cost;
}, $array)));
You can use
array_map: to compare and skip the values for which cost is not needed to add in sum.
array_filter: and then to remove the blank indexes
array_sum: and finally to sum cost
Note: if you are getting results from database, you can simply do sum there (see below):
$db->query("SELECT SUM(costs) FROM table_name WHERE `date`='2016-01-02'");
echo $db->fetchColumn();
Try below code:
<?php
$total = 0;
foreach((array)$query as $row):
$date = $row->date;
$cost = $row->cost;
if(date("M-d-Y", strtotime($date))==date("M-d-Y")) {
$total = $total+$cost;
}
?>
<td><?php echo $date ?></td>
<td><?php echo $cost ?></td>
<?php endforeach; ?>
<td colspan="2"><?php echo $total; ?></td>
<?php
$tot = 0;
foreach( (array)$query as $row ) {
if( $row->date=='02-01-2016' ) $tot = $tot+$row->cost;
?>
<tr>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
</tr>
<?php } ?>
<tr><td colspan="2"><?php echo $tot; ?></td></tr>
obviously, the comparison works if $row->date is formatted as dd-mm-yyyy
<table border=1>
<?php
$tot = 0;
foreach($query as $row ) {
if( $row->date=='2016-01-02' ) $tot += $row->cost;
?>
<tr>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
</tr>
<?php } ?>
<tr><td></td><td><?php echo $total; ?></td></tr>
</table>
It will work for you exctly what u want.