简体   繁体   English

如何为每次迭代在Javascript中添加PHP foreach循环计算值

[英]How to add PHP foreach looping calculated value in Javascript for every iteration

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 我在PHP中有一个查询结果,并使用foreach循环通过在html表中生成新的tr来显示结果,但是与此同时,我想通过添加一些行来使用Javascript显示的循环中将计算出一些值

How can I do this? 我怎样才能做到这一点?

PHP: 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: 使用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 在上面的代码中,如何在JavaScript中添加$bTotalBorn$bTotalSanc值,但不添加总数,同时在javascript中更新循环值

Now showing the grand total like this but need to show subtotal 现在显示这样的总计,但需要显示小计 截图

Can anyone help me? 谁能帮我?

Below an example of what I guess you are trying to do. 下面是我想您正在尝试做的一个例子。

I made a no-PHP example in order for the code snippet to work. 为了使代码段正常工作,我制作了一个无PHP示例。

  • Table rows are generated in the middle of the HTML for corresponding as much as possible at what you did in PHP. 表格行在HTML的中间生成,以尽可能地与您在PHP中所做的相对应。
  • Subtotal cells are generated in Javascript, as in your post. 如您的帖子所述,小计单元格是用Javascript生成的。

My snippet is just here to give you a start solution of your issue. 我的摘录仅在这里为您提供问题的入门解决方案。 I made it in function of how you would like to do (I mainly think of " with the looping value synchronously will be update in javascript ", so usage PHP variables in JS if I am right). 我是根据您的意愿来实现的(我主要认为“ 同步循环值将在javascript中进行更新 ”,因此,如果我正确的话,请在JS中使用PHP变量)。

 $(document).ready(() => { $('table > tbody > tr').each((i, tr) => { const subtotalCellHtml = `<td>${subtotalsA[i]}</td><td>${subtotalsB[i]}</td>`; $(tr).append(subtotalCellHtml); }); }); 
 table { border-collapse: collapse; } table td, table th { border: 1px solid #000; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <thead> <tr> <th>Values A</th> <th>Values B</th> <th>Total A</th> <th>Total B</th> </tr> </thead> <tbody> <!-- generated <tr> --> <!-- below a <script> for imitating your PHP --> <script> /* The code below is in Javascript but should look like your PHP. I placed this code part here because your PHP is in the middle your HTML, I tried to have something similar of your post. */ /** @var {array.<array>} Test values. */ const valuesA = [[ 4, 5, 9, 1 ], [ 2, 1, 8, 11 ]]; const valuesB = [[ 1, 2, 1, 1 ], [ 3, 1, 1, 1, 2 ]]; /** @var {array.<number>} List of subtotalsA. */ let subtotalsA = [], /** @var {array.<number>} List of subtotalsB. */ subtotalsB = [], /** @var {number} Total of all valuesA. */ totalA = 0, /** @var {number} Total of all valuesB. */ totalB = 0; $(document).ready(() => { for (let i in valuesA) { // Line below is the same as looping through valuesA[i] and incrementing a var. const sumA = valuesA[i].reduce((acc, e) => (acc + e)); // Same for valuesB[i] const sumB = valuesB[i].reduce((acc, e) => (acc + e)); subtotalsA.push(sumA); totalA += sumA; subtotalsB.push(sumB); totalB += sumB; // Building table rows without subtotal like in your PHP. const rowHtml = `<tr> <td>${valuesA[i].join(', ')}</td> <td>${valuesB[i].join(', ')}</td> </tr>`; $('table > tbody').append(rowHtml); } }); </script> </tbody> <tfoot> <tr> <th colspan="2">Grand Total</th> <!-- generated <th> --> <script> $(document).ready(() => { const totalCellHtml = `<th>${totalA}</th><th>${totalB}</th>`; $('table > tfoot > tr').append(totalCellHtml); }); </script> </tr> </tfoot> </table> 

If my answer is not correcting your issue (or is not helping you), please tell me, I may have misunderstood what your issue was. 如果我的答案不能解决您的问题(或无法帮助您),请告诉我,我可能误解了您的问题是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM