jquery 如何在html表列中添加值

ehxuflar  于 2023-05-28  发布在  jQuery
关注(0)|答案(2)|浏览(157)

我正在处理以下代码。我需要添加表中每个类别的所有值。数据来自数据库。请先看图片:
Here is my interface image
以下是我的HTML代码:

<table id="data" class="table table-bordered table-striped">
            <thead>
            <tr align="center">
              <th width="5%">Item</th>
              <th width="30%">Description</th>
              <th width="5%">Unit</th>
              <th width="5%">Qty</th>
              <th width="10%">Unit Cost</th>
              <th width="15%">Amount</th>
            </tr>
            </thead>
            <tbody>
              <tr>
                <td>1</td>
                <td colspan="5"><i>General Requirement</i></td>
              </tr>

              <?php  
              $id = $_GET['id'];
              $select_bill = "SELECT *, quantity * unit_cost AS amount FROM bill_tbl WHERE project_name = $id";
              $select_bill_result = mysqli_query($con,$select_bill);
              if (mysqli_num_rows($select_bill_result)> 0) {
                while ($row = mysqli_fetch_assoc($select_bill_result)) {
                  if ($row['category'] == 'General Requirement') {
              ?>

              <tr>
                <td></td>
                <td><?php echo $row['description']; ?></td>
                <td><?php echo $row['unit']; ?></td>
                <td><?php echo $row['quantity']; ?></td>
                <td><?php echo $row['unit_cost']; ?></td>
                <td><?php echo $row['amount']; ?></td>
              </tr>
              <?php
                   }
                }
              }
              ?>
              <tr>
                <td></td>
                <td colspan="4"><b>Subtotal</b></td>
                <td></td>
              </tr>

              <tr>
                <td>2</td>
                <td colspan="5"><i>Concrete and Masonry Works</i></td>
              </tr>

              <?php  
              $id = $_GET['id'];
              $select_bill = "SELECT *, quantity * unit_cost AS amount FROM bill_tbl WHERE project_name = $id";
              $select_bill_result = mysqli_query($con,$select_bill);
              if (mysqli_num_rows($select_bill_result)> 0) {
                while ($row = mysqli_fetch_assoc($select_bill_result)) {
                  if ($row['category'] == 'Concrete and Masonry Works') {
              ?>
              <tr>
                <td></td>
                <td><?php echo $row['description']; ?></td>
                <td><?php echo $row['unit']; ?></td>
                <td><?php echo $row['quantity']; ?></td>
                <td><?php echo $row['unit_cost']; ?></td>
                <td><?php echo $row['amount']; ?></td>
              </tr>
              <?php
                   }
                }
              }
              ?>
              <tr>
                <td></td>
                <td colspan="4"><b>Subtotal</b></td>
                <td></td>  
              </tr>
            </tbody>
          </table>

脚本代码

<script>  
var total = 0;

var rows = $("#data tr:gt(0)");

rows.children("td:nth-child(6)").each(function() {
total += parseInt($(this).html());
});

$("#total").html(total);

</script>

这是我的学校项目。

2o7dmzc5

2o7dmzc51#

你可以在PHP部分求和。像这样的代码将在从DB检索时对所有金额进行求和。

<table id="data" class="table table-bordered table-striped">
        <thead>
        <tr align="center">
          <th width="5%">Item</th>
          <th width="30%">Description</th>
          <th width="5%">Unit</th>
          <th width="5%">Qty</th>
          <th width="10%">Unit Cost</th>
          <th width="15%">Amount</th>
        </tr>
        </thead>
        <tbody>
          <tr>
            <td>1</td>
            <td colspan="5"><i>General Requirement</i></td>
          </tr>

          <?php  
          $id = $_GET['id'];
          $select_bill = "SELECT *, quantity * unit_cost AS amount FROM bill_tbl WHERE project_name = $id";
          $select_bill_result = mysqli_query($con,$select_bill);
          if (mysqli_num_rows($select_bill_result)> 0) {
            while ($row = mysqli_fetch_assoc($select_bill_result)) {
              if ($row['category'] == 'General Requirement') {

              $subtotal += $row['amount']; //Here you sum all amounts from rows
          ?>

          <tr>
            <td></td>
            <td><?php echo $row['description']; ?></td>
            <td><?php echo $row['unit']; ?></td>
            <td><?php echo $row['quantity']; ?></td>
            <td><?php echo $row['unit_cost']; ?></td>
            <td><?php echo $row['amount']; ?></td>
          </tr>
          <?php
               }
            }
          }
          ?>
          <tr>
            <td></td>
            <td colspan="4"><b>Subtotal</b></td>
            <td><?php echo $subtotal; //Here you display subtotal ?></td>
          </tr>

          <tr>
            <td>2</td>
            <td colspan="5"><i>Concrete and Masonry Works</i></td>
          </tr>

          <?php  
          $id = $_GET['id'];
          $select_bill = "SELECT *, quantity * unit_cost AS amount FROM bill_tbl WHERE project_name = $id";
          $select_bill_result = mysqli_query($con,$select_bill);
          if (mysqli_num_rows($select_bill_result)> 0) {
            while ($row = mysqli_fetch_assoc($select_bill_result)) {
              if ($row['category'] == 'Concrete and Masonry Works') {

              $subtotal += $row['amount']; //Here you sum all amounts from rows
          ?>
          <tr>
            <td></td>
            <td><?php echo $row['description']; ?></td>
            <td><?php echo $row['unit']; ?></td>
            <td><?php echo $row['quantity']; ?></td>
            <td><?php echo $row['unit_cost']; ?></td>
            <td><?php echo $row['amount']; ?></td>
          </tr>
          <?php
               }
            }
          }
          ?>
          <tr>
            <td></td>
            <td colspan="4"><b>Subtotal</b></td>
            <td><?php echo $subtotal; //Here you display subtotal ?></td>  
          </tr>
        </tbody>
      </table>
2ic8powd

2ic8powd2#

<table id="data" class="table table-bordered table-striped">
        <thead>
        <tr align="center">
          <th width="5%">Item</th>
          <th width="30%">Description</th>
          <th width="5%">Unit</th>
          <th width="5%">Qty</th>
          <th width="10%">Unit Cost</th>
          <th width="15%">Amount</th>
        </tr>
        </thead>
        <tbody>
          <tr>
            <td>1</td>
            <td colspan="5"><i>General Requirement</i></td>
          </tr>

          <?php  
          $id = $_GET['id']; $catA =0; //this variable will be use to get the some of the category. You can name the variable whatever name you want
          $select_bill = "SELECT *, quantity * unit_cost AS amount FROM bill_tbl WHERE project_name = $id";
          $select_bill_result = mysqli_query($con,$select_bill);
          if (mysqli_num_rows($select_bill_result)> 0) {
            while ($row = mysqli_fetch_assoc($select_bill_result)) {
              if ($row['category'] == 'General Requirement') {
                $catA+=$row['amount']; // this will add each amount value to the $catA variable until the loop finished;
          ?>

          <tr>
            <td></td>
            <td><?php echo $row['description']; ?></td>
            <td><?php echo $row['unit']; ?></td>
            <td><?php echo $row['quantity']; ?></td>
            <td><?php echo $row['unit_cost']; ?></td>
            <td><?php echo $row['amount']; ?></td>
          </tr>
          <?php
               }
            }
          }
          ?>
          <tr>
            <td></td>
            <td colspan="4"><b>Subtotal</b></td>
            <td><?php echo $catA; //the variable now has the total sum of the amount;?></td>
          </tr>

          <tr>
            <td>2</td>
            <td colspan="5"><i>Concrete and Masonry Works</i></td>
          </tr>

          <?php  
          $id = $_GET['id'];  $catB =0; //this variable will be use to get the some of the category. You can name the variable whatever name you want
          $select_bill = "SELECT *, quantity * unit_cost AS amount FROM bill_tbl WHERE project_name = $id";
          $select_bill_result = mysqli_query($con,$select_bill);
          if (mysqli_num_rows($select_bill_result)> 0) {
            while ($row = mysqli_fetch_assoc($select_bill_result)) {
              if ($row['category'] == 'Concrete and Masonry Works') {
                 $catB+=$row['amount']; // this will add the amount value to the $catB variable until the loop finished; 
          ?>
          <tr>
            <td></td>
            <td><?php echo $row['description']; ?></td>
            <td><?php echo $row['unit']; ?></td>
            <td><?php echo $row['quantity']; ?></td>
            <td><?php echo $row['unit_cost']; ?></td>
            <td><?php echo $row['amount']; ?></td>
          </tr>
          <?php
               }
            }
          }
          ?>
          <tr>
            <td></td>
            <td colspan="4"><b>Subtotal</b></td>
            <td><?php echo $catB; //the variable now has the total sum of the amount;?></td>  
          </tr>
        </tbody>
      </table>

相关问题