php - DataTables-PHP while循环问题找不到最后一个结果并添加新的TR

我正在使用DataTables,并且我想在while循环的末尾添加新的TR

我知道我们可以添加<tfoot></tfoot>,但是我不想添加”,因为我正在使用自定义Ajax过滤数据。

我已经尝试过下面的代码,但是不能正常工作:

<?php
    $Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
    while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
    <thead>
      <tr>
        <th>Group</th>
        <th>Party Name</th>
        <th>Balance</th> 
      </tr>
    </thead>
    <tbody id="getGroups">
    <?php
        $i = 1;
        while($row = mysqli_fetch_array($sdetails)){
        $totalAmount += $row['total_debtors'];
        $i++;
    ?>  
        <tr>
            <td><a href="view_debtors_area.php?auth=<?php echo $salt_key_check.'&lid='.$row['area_id']; ?>"><?php echo getAreaName($row['area_id']); ?></a></td>
            <td><?php echo GrabAccountIDName($row['client_id']); ?></td>
            <td><?php echo abs($row['total_debtors']); ?></td>
        </tr>
    <?php if( $i == ( $numRows  - 1 ) ) { ?>            
            <tr>
                <td>&nbsp;</td>
                <td style="text-align:right">Total:</td>
                <td><?php echo abs($totalAmount); ?></td>
            </tr>

    <?php }  }  ?>          
    </tbody>
</table>


另外,当我使用 <tfoot></tfoot>时,它是不可打印的。

最佳答案

可能您的问题出在未定义的$numRows中。

因此,您可以尝试以下操作:

<?php
    $Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
    $numRows = mysqli_num_rows($Itesres);
    while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
    <thead>
      <tr>
        <th>Group</th>
        <th>Party Name</th>
        <th>Balance</th> 
      </tr>
    </thead>
    <tbody id="getGroups">
    <?php
        $i = 1;
        while($row = mysqli_fetch_array($sdetails)){
        $totalAmount += $row['total_debtors'];
        $i++;
    ?>  
        <tr>
            <td><a href="view_debtors_area.php?auth=<?php echo $salt_key_check.'&lid='.$row['area_id']; ?>"><?php echo getAreaName($row['area_id']); ?></a></td>
            <td><?php echo GrabAccountIDName($row['client_id']); ?></td>
            <td><?php echo abs($row['total_debtors']); ?></td>
        </tr>
    <?php if( $i == ( $numRows  - 1 ) ) { ?>            
            <tr>
                <td>&nbsp;</td>
                <td style="text-align:right">Total:</td>
                <td><?php echo abs($totalAmount); ?></td>
            </tr>

    <?php }  }  ?>          
    </tbody>
</table>