我如何在foreach循环中实现3个输入的总和?

  • 本文关键字:3个 实现 foreach 循环 php
  • 更新时间 :
  • 英文 :


基本上我在foreach循环上工作以获取用户,我假设从数据库中有6个,然后这6个用户中的每一个都有3个输入,其中3个输入的值必须相互求和。我想让这6个用户的total_sum不同。我该怎么做呢?这是我的代码

<tbody>
<form action="payment-code.php" method="POST">
<?php 
$query = "SELECT * FROM staffs";
$query_run = mysqli_query($conn, $query); 
if(mysqli_num_rows($query_run) > 0){
foreach($query_run as $rows)
{
?>
<tr>
<td>
<?=$rows['staff_id']; ?>
</td>
<td>
<?=$rows['firstname'] . $rows['surname']; ?>
</td>
<td>
<input type="text" name="salary" class="form-control" placeholder="salary">
</td>
<td>
<input type="text" name="totalbonus" class="form-control" placeholder="total bonus">
</td>
<td>
<input type="text" name="totalfee" class="form-control" placeholder="total fee">
</td>
<?php
}
else{
?>
<td colspan="5">No record found!</td>
<?php
}
}
?>
</tr>
</form>
</tbody>

我想把3(工资+奖金总额+费用总额)加起来,得到每个人的总金额。我怎样才能做到呢?

希望对大家有所帮助

<form action="payment-code.php" method="POST">
<table>
<?php 
$query = "SELECT * FROM staffs";
$query_run = mysqli_query($conn, $query); 
if(mysqli_num_rows($query_run) > 0) {

foreach($query_run as $rows) {
echo `     
<tr>
<td>
` . $rows['staff_id'] . `
</td>
<td>
` . $rows['firstname'] . $rows['surname'] . `
</td>
<td>
<input type="text" name="salary" class="form-control" placeholder="salary">
</td>
<td>
<input type="text" name="totalbonus" class="form-control" placeholder="total bonus">
</td>
<td>
<input type="text" name="totalfee" class="form-control" placeholder="total fee">
</td>`;
}


}

else {echo `<td colspan="5">No record found!</td>`;}
?>
</table>

或者不带echo

<tbody>
<form action="payment-code.php" method="POST">
<?php 
$query = "SELECT * FROM staffs";
$query_run = mysqli_query($conn, $query); 
if(mysqli_num_rows($query_run) > 0){
foreach($query_run as $rows)
{
?>
<tr>
<td>
<?= $rows['staff_id']; ?>
</td>
<td>
<?=$rows['firstname'] . $rows['surname']; ?>
</td>
<td>
<input type="text" name="salary" class="form-control" placeholder="salary">
</td>
<td>
<input type="text" name="totalbonus" class="form-control" placeholder="total bonus">
</td>
<td>
<input type="text" name="totalfee" class="form-control" placeholder="total fee">
</td>
<?php
}

}
else {
?>
<td colspan="5">No record found!</td>
<?php
}
?>
</tr>
</form>
</tbody>

最新更新