我有一个正在使用的小脚本,可以在一个漂亮的表中生成收入和支出报告,但是,我目前面临排序顺序方面的一些问题。这是我当前的代码:
<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']
?>
<tbody>
<tr>
<td><?php echo date('d/m/Y', strtotime($row["ExpenseDate"]));?></td>
<td><?php echo $row['ExpenseItem'];?></td>
<td><?php echo $row['ExpenseCat'];?></td>
<td></td>
<td style="text-align:right;"><?php echo number_format($row['ExpenseCost'], 2);?> THB</td>
<td style="text-align:right;"><?php echo number_format($row['ExpenseCost'], 2);?> THB</td>
</tr>
<?php
$cnt=$cnt+1;
}?>
<?php
$total_i =0;
$userid=$_SESSION['detsuid'];
$ret=mysqli_query($con,"select * from tblexpense where (ExpenseDate BETWEEN '$fdate' and '$tdate') && (ExpenseType='Income') && UserId='$userid' ORDER BY ExpenseDate ASC");
$cnt=1;
while ($row=mysqli_fetch_array($ret)) {
$total_i = $total_i+$row['ExpenseCost'];
?>
<tbody>
<tr>
<td><?php echo date('d/m/Y', strtotime($row["ExpenseDate"]));?></td>
<td><?php echo $row['ExpenseItem'];?></td>
<td></td>
<td style="text-align:right;"><?php echo number_format($row['ExpenseCost'], 2);?> THB</td>
<td></td>
<td style="text-align:right;"><?php echo number_format($row['ExpenseCost'], 2);?> THB</td>
</tr>
<?php
$cnt=$cnt+1;
}?>
<tr>
<th colspan="2" style="text-align:left">Grand Total</th>
<th></th>
<th style="text-align:right;"><?php echo number_format($total_i, 2);?> THB</th>
<th style="text-align:right;"><?php echo number_format($total_e, 2);?> THB</th>
<td style="text-align:right; font-weight:bold; background: yellow;"> <?php $ret=mysqli_query($con,"SELECT ExpenseType, SUM(ExpenseCost) FROM tblexpense where (ExpenseDate BETWEEN '$fdate' and '$tdate') && (ExpenseType='Expense') && UserId='$userid' ORDER BY ExpenseDate GROUP BY ExpenseType");
$e_sum = 0;
while ($row=mysqli_fetch_array($ret)) {
//$e_sum = number_format($row['SUM(ExpenseCost)'], 2);
$e_sum = $row['SUM(ExpenseCost)'];
}
?>
<?php $ret=mysqli_query($con,"SELECT ExpenseType, SUM(ExpenseCost) FROM tblexpense where (ExpenseDate BETWEEN '$fdate' and '$tdate') && (ExpenseType='Income') && UserId='$userid'ORDER BY ExpenseDate GROUP BY ExpenseType");
$i_sum = 0;
while ($row=mysqli_fetch_array($ret)) {
//$i_sum = number_format($row['SUM(ExpenseCost)'], 2);
$i_sum = $row['SUM(ExpenseCost)'];
}
echo number_format($i_sum-$e_sum,2);
?> THB</td>
</th>
</tr>
</tbody>
</table>
<!-- table end--->
</div>
</div>
下面是输出的屏幕截图:
从本质上来说,输出和值是正确的,但是,我用红色标记的2个项目(即套现)没有按日期排序,而与我的其他支出值相同,但都位于顶部。我的理想目标是让它们全部通过ExpenseDate无缝排序。实现此目标的最佳方法是什么?一些专家的帮助将不胜感激。
答案 0 :(得分:1)
您应该将两个查询合并为一个,然后根据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>