Home > Back-end >  How do I achieve total sum of 3 input inside the foreach loop?
How do I achieve total sum of 3 input inside the foreach loop?

Time:06-11

basically I work on foreach loop to fetch users, i assume having 6 from database and then each of those 6 users have 3 input each, which values from 3 of those input have to sum with each other. I want to have total_sum differently between those 6 users. How do i achieve that? Here are my code

<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"  placeholder="salary">
         </td>
         <td>
            <input type="text" name="totalbonus"  placeholder="total bonus">
         </td>
         <td>
            <input type="text" name="totalfee"  placeholder="total fee">
         </td>
       <?php
               }
             else{
               ?>
              <td colspan="5">No record found!</td>
            <?php
        }
       }
       ?>
       </tr>
</form>
</tbody>

I want to sum 3 of (salary total bonus total fee ) and get total amount inside foreach. How can i achieve that?

CodePudding user response:

Hope this will help

 <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"  placeholder="salary">
    </td>
    <td>
        <input type="text" name="totalbonus"  placeholder="total bonus">
    </td>
    <td>
        <input type="text" name="totalfee"  placeholder="total fee">
    </td>`;
                   }
    
    
           }
    
    else {echo `<td colspan="5">No record found!</td>`;}
           ?>
    </table>

CodePudding user response:

Or without 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"  placeholder="salary">
         </td>
         <td>
            <input type="text" name="totalbonus"  placeholder="total bonus">
         </td>
         <td>
            <input type="text" name="totalfee"  placeholder="total fee">
         </td>
       <?php
               }
            
       }
       else {
        ?>
       <td colspan="5">No record found!</td>
     <?php
 }
       ?>
       </tr>
</form>
</tbody>
  • Related