【问题标题】:How to echo Month wise data in PHP MySqli? [closed]如何在 PHP MySqli 中回显月份数据? [关闭]
【发布时间】:2023-03-25 21:25:01
【问题描述】:

我有一个简单的问题,如何回显月份数据?每个月通常有 2-3 头费用。例如,学费+学期费+活动费。

我想回显 4 月、7 月、8 月等所有存储值在 <table></table> 中。为此,我使用了两个 While 循环,第一个用于选择月份和类,第二个循环位于第一个循环内,用于在所选月份内打印值。但是循环多次打印相同的最高值。

例如,Apr 有 3 个头,在数据库表 Tution fee 上,现在循环打印 Tution fee 3 次。

请帮我解决这个问题。

代码如下:

<table  class="table table-bordered responsive">
<tr>
<th colspan="2">
Fees Detail</th>
</tr>
<tr>
<th>Fee Name</th>
<th>Amount</th>
<th>Status</th>
</tr>
<?php
$data = mysqli_query($conn,"SELECT * FROM fees_on_class WHERE class = '$qry[class]' GROUP BY month_id ");
while($row = mysqli_fetch_array($data)) 
{

echo "<tr>";
echo "<th colspan='3'>"."<input type='checkbox' id='selectall'/>". "Month:- ".$row['month']."</th>";
echo "</tr>";

$d = mysqli_query($conn,"SELECT * FROM fees_on_class WHERE month = '$row[month]' AND class = '$row[class]' ");
while($r = mysqli_fetch_array($d)) 
{
echo "<tr>";
echo "<td>"."<input type='checkbox' class='case' name='case' value='1'/>".$row['fees_head']."</td>";
echo "<td>".$row['amount'];"</td>";
echo "<td>"."status"."</td>";
echo "</tr>";
}
}
?>
</table>

【问题讨论】:

    标签: php mysql mysqli


    【解决方案1】:

    最简单的方法是

    SELECT ..., year(datefield), month(datefield)
    FROM yourtable
    WHERE datefield BETWEEN $start AND $end
    GROUP BY year(datefield), month(datefield)
    ORDER BY year(datefield), month(datefield)
    

    然后

    while($row = fetch row from db) {
       if (is new year) { start new year table }
       if (is new month) { start new month headers }
        ... output data for row
    }
    

    循环内无需查询 - 您只需运行一个查询并根据需要调整输出逻辑。

    【讨论】:

    猜你喜欢
    • 2014-07-11
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2018-08-16
    • 1970-01-01
    • 2016-05-07
    相关资源
    最近更新 更多