Jacky
Jacky

Reputation: 781

How to calculate the previous row and show in the current row of the table?

I am fetching some data from the table using codeigniter

My table picture enter image description here This is my table that i have fetched from the table.in this i have show the balance column in first row as 10 and next row as 35 how i calculate this using java script or php itself.

My code:

<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>

This is my table code. My another sample table: enter image description here

Upvotes: 0

Views: 159

Answers (1)

Devsi Odedra
Devsi Odedra

Reputation: 5322

Define $balance before foreach, and sum as $balance += $total; every row of balance column.

 <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>

Upvotes: 1

Related Questions