猿问

数组中按日期排序的 PHP 问题

我有一个小脚本,我正在处理它,我可以在一个漂亮的表格中生成收入和支出报告,但是,我目前面临着排序顺序的一些问题。这是我当前的代码:


    <div class="panel-body">

            <div class="col-md-12">


            <?php

            $fdate=$_POST['fromdate'];

            $tdate=$_POST['todate'];

            $rtype=$_POST['requesttype'];

            ?>


            <?php

                $source1 = $_POST['fromdate'];

                $date1 = new DateTime($source1);

                $source2 = $_POST['todate'];

                $date2 = new DateTime($source2);


            ?>


            <h5 align="center" style="color:#30a5ff">Expense Report from <?php echo $date1->format('d/m/Y')?> to <?php echo $date2->format('d/m/Y')?></h5>

            <hr />



            <!-- table start--->


            <table class="table table-bordered mg-b-0">

              <thead>

                <tr>

                  <th>Date</th>

                  <th>Description</th>

                  <th>Category</th>

                  <th style="text-align:right;">Cash In</th>

                  <th style="text-align:right;">Cash Out</th>

                  <th style="text-align:right;background: yellow;">Balance THB</th>


                </tr>

              </thead>


              <?php

                $total_e = 0;

                $userid=$_SESSION['detsuid'];

                $ret=mysqli_query($con,"select * from tblexpense where (ExpenseDate BETWEEN '$fdate' and '$tdate')  && (ExpenseType='Expense') && UserId='$userid' ORDER BY ExpenseDate ASC");

                $cnt=1;

                while ($row=mysqli_fetch_array($ret)) {

                $total_e = $total_e+$row['ExpenseCost']


以下是输出的屏幕截图:

基本上输出和值是正确的,但是,我用红色标记的2个项目(即“提现”)没有按日期排序,因为我的其他费用值,而是添加到顶部。我的理想目标是让它们都按费用日期无缝排序。实现这一目标的最佳方式是什么?一些专家的帮助将不胜感激。


叮当猫咪
浏览 110回答 1
1回答

GCT1015

您应该将两个查询合并为一个,然后根据例如ExpenseType$ret=mysqli_query($con,"select *&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from tblexpense&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where (ExpenseDate BETWEEN '$fdate' and '$tdate')&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and (ExpenseType='Expense' or ExpenseType='Income')&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and UserId='$userid'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ORDER BY ExpenseDate ASC");然后在您的循环中:<tr>&nbsp; <td><?php&nbsp; echo date('d/m/Y', strtotime($row["ExpenseDate"]));?></td>&nbsp; <td><?php&nbsp; echo $row['ExpenseItem'];?></td>&nbsp; <td><?php&nbsp; echo $row['ExpenseType'] = 'Expense' ? $row['ExpenseCat'] : '';?></td>&nbsp; <td style="text-align:right;"><?php&nbsp; echo&nbsp; $row['ExpenseType'] = 'Expense' ? '' : number_format($row['ExpenseCost'], 2); ?> THB</td>&nbsp; <td style="text-align:right;"><?php&nbsp; echo&nbsp; $row['ExpenseType'] = 'Expense' ? number_format($row['ExpenseCost'], 2) : '';?> THB</td>&nbsp; <td style="text-align:right;"><?php&nbsp; echo number_format($row['ExpenseCost'], 2);?> THB</td></tr>
随时随地看视频慕课网APP
我要回答