如何计算表格的上一行并显示在表格的当前行中?

时间:2019-12-07 07:30:09

标签: javascript codeigniter

我正在使用codeigniter从表中获取一些数据

我的桌子图片 enter image description here 这是我从table中获取的表。在此表中,第一行的余额列显示为10,第二行的余额列显示为35,我如何使用Java脚本或php本身来计算此余额。

我的代码:

<table class="table datatable-button-html5-basic">
    <thead>
    <tr><h6>         
        <th class="col-sm">Bill No</th>
        <th class="col-sm" >Date</th>
        <th class="col-sm" >Particulars</th>
        <th class="col-sm" >Op.Stock</th>
        <th class="col-sm-7">Pur/Prod</th>
        <th class="col-sm" >Sales/Cons</th>
        <th class="col-sm" >Balance</th>        

</tr>
</thead>
<tbody>
        <?php foreach ($query as $row): ?>
<tr>
     <?php $total = 0;
          $total += $row['qty'];  ?>
        <td>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['orderno'];?></td>
        <td ><?php echo $row['orderdate'];?></td>
        <td >PRODUCTION</td>
        <td></td>       
        <td dir="rtl"><?php echo $row['qty'];?></td>
        <td></td>
        <td><?php echo "$total" ;?></td>

</tr><?php endforeach ?>
<tr><?php foreach ($query1 as $row1): ?>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row1['billno'];?></td>
        <td ><?php echo $row1['billdate'];?></td>
        <td ><?php echo $row1['accName'];?></td>
        <td></td>       
        <td></td>
        <td dir="rtl"><?php echo $row1['salesqty'];?></td>
        <td></td>
</tr>
<?php endforeach ?>
</tbody>

这是我的表格代码。 我的另一个示例表:enter image description here

1 个答案:

答案 0 :(得分:1)

$balance之前定义foreach,并将余额列的每一行总计为$balance += $total;

 <table class="table datatable-button-html5-basic">
        <thead>
        <tr><h6>         
            <th class="col-sm">Bill No</th>
            <th class="col-sm" >Date</th>
            <th class="col-sm" >Particulars</th>
            <th class="col-sm" >Op.Stock</th>
            <th class="col-sm-7">Pur/Prod</th>
            <th class="col-sm" >Sales/Cons</th>
            <th class="col-sm" >Balance</th>        

    </tr>
    </thead>
    <tbody>
            <?php

$balance = 0;
 foreach ($query as $row): ?>
    <tr>
         <?php $total = 0;
              $total += $row['qty']; 
             $balance += $total;
 ?>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['orderno'];?></td>
            <td ><?php echo $row['orderdate'];?></td>
            <td >PRODUCTION</td>
            <td></td>       
            <td dir="rtl"><?php echo $row['qty'];?></td>
            <td></td>
            <td><?php echo "$total" ;?></td>

    </tr><?php endforeach ?>
    <tr><?php foreach ($query1 as $row1): ?>
        <td>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row1['billno'];?></td>
            <td ><?php echo $row1['billdate'];?></td>
            <td ><?php echo $row1['accName'];?></td>
            <td></td>       
            <td></td>
            <td dir="rtl"><?php echo $row1['salesqty'];?></td>
            <td></td>
    </tr>
    <?php endforeach ?>
    </tbody>