I have a query result in PHP and using foreach loop showing the result by generating new tr in html table but at the same time some value will be calculated inside the loop that I want to show using Javascript by adding some rows
How can I do this?
PHP:
<tbody>
<?php
$gTotalBorn = 0;
$gTotalSanc = 0;
$bTotalBorn = 0;
$bTotalSanc = 0;
foreach ($reNominalRoll as $key => $value) {
if ($row_area->ADMIN_ID == $value->AREA_ID) {
$gTotalBorn += $value->Borne;
$gTotalSanc += $value->sanction;
?>
<tr class="allrow">
<td><?php echo $value->Rank ?></td>
<td><?php echo $value->Part ?></td>
<td><?php echo $value->sanction;
$bTotalSanc += $value->sanction; ?></td>
<td><?php echo $value->Borne;
$bTotalBorn += $value->Borne; ?></td>
<td><?php echo $value->TotalIn ?></td>
<td><?php echo $value->TotalOut ?></td>
</tr>
<?php
}
}
}
?>
<!--Grand Total Row-->
<tr>
<td style="text-align: center; font-weight: bold;" colspan="5">Grand Total</td>
<td style="text-align: center; font-weight: bold;"><?php echo $gTotalSanc; ?></td>
<td style="text-align: center; font-weight: bold;"><?php echo $gTotalBorn; ?></td>
<td></td>
<td></td>
</tr>
</tbody>
Javascript:
if (allrow.hasClass('expectedRow')) {
$('.expectedRow').each(function () {
let thisRow = $(this);
let prevShip = thisRow.prevAll('.ships:first');
let prevUnit = thisRow.prevAll('.units:first');
let prevBranch = thisRow.prevAll('.units:first');
//thisRow.after('<tr><td colspan="6"><strong>Branch Total</strong></td></tr>');
thisRow.after('<tr>'+
'<td><strong>Branch Total</strong></td>'+
'<td></td>'+
'<td><strong>'+"<?php echo $bTotalSanc; $bTotalSanc = 0;?>"+'</strong></td>'+
'<td><strong>'+"<?php echo $bTotalBorn; $bTotalBorn = 0;?>"+'</strong></td>'+
'<td></td>'+
'<td></td>'+
'</tr>');
// below conditions for identify the unit based on rowspan length
if (prevUnit.find('td[rowspan]').length == 2) {
// ship rowspan incremented by 1
let prevShipRowspan = parseInt(prevShip.find('td:first').attr('rowspan'));
let newPrevShipRowspan = prevShipRowspan+1;
prevShip.find('td:first').attr('rowspan',newPrevShipRowspan);
// unit rowspan incremented by 1
let unitRowspan = parseInt(prevUnit.find('td:first').attr('rowspan'));
let newUnitRowspan = unitRowspan+1;
prevUnit.find('td:first').attr('rowspan',newUnitRowspan);
// branch rowspan incremented by 1
let branchRowspan = parseInt(prevUnit.find('td:eq(1)').attr('rowspan'));
let newBranchRowspan = branchRowspan+1;
prevBranch.find('td:eq(1)').attr('rowspan',newBranchRowspan);
}
else if (prevUnit.find('td[rowspan]').length == 3) {
// ship rowspan incremented by 1
let prevShipRowspan = parseInt(prevShip.find('td:first').attr('rowspan'));
let newPrevShipRowspan = prevShipRowspan+1;
prevShip.find('td:first').attr('rowspan',newPrevShipRowspan);
// unit rowspan incremented by 1
let unitRowspan = parseInt(prevUnit.find('td:eq(1)').attr('rowspan'));
let newUnitRowspan = unitRowspan+1;
prevUnit.find('td:eq(1)').attr('rowspan',newUnitRowspan);
// branch rowspan incremented by 1
let branchRowspan = parseInt(prevBranch.find('td:eq(2)').attr('rowspan'));
let newBranchRowspan = branchRowspan+1;
prevBranch.find('td:eq(2)').attr('rowspan',newBranchRowspan);
}
});
}
In the above code, how to add $bTotalBorn and $bTotalSanc value add in javascript but not total, with the looping value synchronously will be update in javascript
Now showing the grand total like this but need to show subtotal

Can anyone help me?
.each()).