如何在PHP MySqli中回显月份数据?

时间:2015-08-14 20:53:38

标签: php mysql mysqli

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

我希望在4月,7月,8月......以及<table></table>内回复所有存储的值。为此,我使用两个While循环,第一个用于选择月份和类,第二个循环在第一个循环内,用于在所选月份内打印值。但循环多次打印相同的最高值。

例如,4月有3个头,并且在数据库表中,Tution费用在上面,现在循环是3次打印Tution费用。

请帮助我。

以下是代码:

<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>

1 个答案:

答案 0 :(得分: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
}

不需要在循环内部进行查询 - 运行单个查询并根据需要调整输出逻辑。