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

问题描述 投票:0回答:1

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

我的桌子图片enter image description here这是我从table.fetch中获取的表。在此表中,第一行的余额列显示为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

javascript codeigniter
1个回答
0
投票

$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>
© www.soinside.com 2019 - 2024. All rights reserved.