dimanche 28 octobre 2018

PHP - Show calculation only when IF statement changes

Here is my code:

        <table class="table table-bordered">
        <thead>
            <tr>
                <th>Sr#</th>
                <th>Description</th>
                <th>Sale</th>
                <th>Expense</th>
            </tr>
        </thead>    
        <tbody>
            <?php
            $fetch_record = "SELECT * FROM `transactions` JOIN `users` ON `transactions`.`user_id`=`users`.`user_id` WHERE `users`.`user_id`='$user_id'";
            $fetch_record_r = mysqli_query($con, $fetch_record);
            $record_found = mysqli_num_rows($fetch_record_r);
            if ($record_found > 0) {
                $sr = 1;
                $net_total = 0;
                while($records = mysqli_fetch_assoc($fetch_record_r)){
                    if ($records['type'] == 1) {
                        $net_total = $net_total + $records['sale'];
                    }else if($records['type'] == 2){
                        $net_total = $net_total - $records['expense'];
                    }
            ?>
                    <tr>
                        <td><?php echo $sr++; ?></td>
                        <td style="text-transform: capitalize;"><?php echo $records['description']; ?></td>
                        <td><?php echo $records['sale'];?></td>
                        <td><?php echo $records['expense'];?></td>
                    </tr>
                    <?php
                    if (($sr % 2)) { // skip even members
                        ?>
                        <tr>
                            <td colspan="2"></td>
                            <td colspan="2" class="label-info text-center" style="color: #fff;">Total Balance: <?php echo $net_total; ?></td>
                        </tr>
                        <?php
                    }
                    ?>
            <?php
                }
                    ?>
                    <tr>
                        <td colspan="4" class="label-info text-center" style="color: #fff;">Total: <?php echo $net_total; ?></td>
                    </tr>
                    <?php
            }else{
                echo "<h1 class='text-center text-danger'>No Record Found</h1>";
            }
            ?>
        </tbody>
    </table>

Data is coming from database but not the calculations. I want when the transaction type changes from sale to expense then all the sales related transactions should be calculated and vice versa. Just like an balance sheet.I know i have written a wrong logic. but i am unable to tackle the logic. You can understand my question more clearly by seeing the image.Sale expense calculator example

Aucun commentaire:

Enregistrer un commentaire