亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

數組中按日期排序的 PHP 問題

數組中按日期排序的 PHP 問題

PHP
叮當貓咪 2022-09-17 22:09:16
我有一個小腳本,我正在處理它,我可以在一個漂亮的表格中生成收入和支出報告,但是,我目前面臨著排序順序的一些問題。這是我當前的代碼:    <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個項目(即“提現”)沒有按日期排序,因為我的其他費用值,而是添加到頂部。我的理想目標是讓它們都按費用日期無縫排序。實現這一目標的最佳方式是什么?一些專家的幫助將不勝感激。
查看完整描述

1 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

您應該將兩個查詢合并為一個,然后根據例如ExpenseType


$ret=mysqli_query($con,"select * 

                        from tblexpense 

                        where (ExpenseDate BETWEEN '$fdate' and '$tdate')  

                          and (ExpenseType='Expense' or ExpenseType='Income') 

                          and UserId='$userid'

                        ORDER BY ExpenseDate ASC");

然后在您的循環中:


<tr>

  <td><?php  echo date('d/m/Y', strtotime($row["ExpenseDate"]));?></td>

  <td><?php  echo $row['ExpenseItem'];?></td>

  <td><?php  echo $row['ExpenseType'] = 'Expense' ? $row['ExpenseCat'] : '';?></td>

  <td style="text-align:right;"><?php  echo  $row['ExpenseType'] = 'Expense' ? '' : number_format($row['ExpenseCost'], 2); ?> THB</td>

  <td style="text-align:right;"><?php  echo  $row['ExpenseType'] = 'Expense' ? number_format($row['ExpenseCost'], 2) : '';?> THB</td>

  <td style="text-align:right;"><?php  echo number_format($row['ExpenseCost'], 2);?> THB</td>

</tr>


查看完整回答
反對 回復 2022-09-17
  • 1 回答
  • 0 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號