PHP MYSQL如何查看数量是DR还是CR?

时间:2019-05-08 10:09:12

标签: php mysql

我已经使用PHP MYSQLi创建了资产负债表系统。管理员可以在哪里管理客户帐户信息

插入数据时,我正在使用列action_type

下面是我的表结构。

enter image description here

我已经尝试过以下代码来获取DR和CR:

<?php
$list_balance = mysqli_query($con_db,"SELECT * FROM tbl_balancesheet where client_id='".$_REQUEST['clientID']."' AND YEAR(created_date) = '".$_REQUEST['id']."' ORDER by id asc" );
    $numRows = mysqli_num_rows($list_balance);
    $total = 0;
    $totalAmount = 0;
    $i = 1;
    while($row = mysqli_fetch_array($list_balance)){
        $totalAmount += $row['amount']; 

        if($row['action_type']=='dr'){
            $debit = $row['amount'];
            $total += $row['amount'];
        }else{
            $credit = $row['amount'];
            $total -= $row['amount']; 
        }

        if($totalAmount < $total){
            $checkAmount = 'dr';
        }else{
            $checkAmount = 'cr';
        }

    ?>
    <tr>
        <td width="5%">
        <a href='edit_particular.php?auth=<?php echo $salt_key_check.'&cid='.$row['id']; ?>' role='button' title='Edit' class='btn btn-warning btn-xs'><i class='fa fa-edit'></i></a>
          <a href='delete_particular.php?auth=<?php echo $salt_key_check.'&cid='.$row['id'];?>&client_id=<?php echo $row['client_id'] ?>&lid=<?php echo $row['area_id']; ?>' role='button' title='Delete' class='btn btn-danger btn-xs'><i class='fa fa-trash-o'></i></a>   
        </td>
        <td><?php echo date('d-m-Y', strtotime($row['created_date'])); ?></td>
        <td><?php echo $row['particular']; ?>
        </td>
        <td>
            <?php 
                if($row['action_type']=='dr'){ 
                    echo $debit;
                }else{
                    echo '';        
                }
            ?>
        </td>
        <td>
            <?php 
                if($row['action_type']=='cr'){ 
                    echo $credit;
                }else{
                    echo '';        
                }
            ?>
        </td>
        <td>
        <?php if ($i < $numRows)
            {
        ?><?php echo $total; ?>&nbsp;<small><?php //echo $row['action_type']; ?></small>
        <?php }else{ ?>
        <strong><?php echo $total; ?></strong>&nbsp;<small><?php echo $checkAmount; ?></small>
        <?php } ?>
        </td>
    </tr>
<?php 
    $i++; }  
?>

以下是预期结果:

enter image description here

请帮助!!!

3 个答案:

答案 0 :(得分:0)

我的假设是,您发布的结果就是您现在得到的结果;而您的问题是仅最后一行显示CR,而其他行不显示CR / DR文本。

为什么不尝试反转if条件。

if ($i < $numRows)

收件人

if ($i >= $numRows)

答案 1 :(得分:0)

if($totalAmount < $total){ 总是错误的。为什么?因为$totalAmount += $row['amount'];一直是正数,所以amount一直在增加。即使所有交易都是灾难恢复;在这种情况下,$totalAmount == $total

您是否完全需要$totalAmount?如果$total < 0,则$checkAmount = 'cr',句号停止。

答案 2 :(得分:0)

我已经在while循环外使用以下查询完成了此操作:

$sqlDRCR = mysqli_query($con_db,"SELECT id,area_id,client_id, 
                   IF(Sum(Coalesce(CASE 
                                     WHEN action_type = 'dr' THEN amount 
                                   end, 0)) - Sum(Coalesce(CASE 
                                                             WHEN action_type = 'cr' THEN 
                                                             amount 
                                                           end, 0)) > 0, 'dr', 'cr') AS balance 
            FROM tbl_balancesheet WHERE `client_id` = '".$_GET['cid']."'");
            $resDRCR = mysqli_fetch_array($sqlDRCR);