我有以下代码查询数据库并显示结果:
<?php
include('connect.php');
$query="SELECT date1,month(date1) as month1,year(date1)as month2,(month( date1 )-month(date1 )) as difference,SUM(gtotal) AS daily_total,COUNT(id) AS num_orders FROM `sale`
where date1>='2012-01-28' AND date1<='2013-01-31' and tax_name='C.S.T. 4% ON SCRAP' and tin='Applied For' GROUP BY date1 ";
$qt=mysql_query($query);
echo mysql_error();
echo "<table border='1' cellspacing='1' cellpadding='0' width='400'>
<tr valign='top' bgcolor=#363636 style='color:#ffffff;'>
<td><b>No. of orders</b></td>
<td><b>Date</b></td>
<td><b>Month<br>(dt)</b></td>
<td><b>Year<br>(dt2)</b></td>
<td><b>difference</b></td>
</tr>";
while($nt=mysql_fetch_array($qt))
{
echo "<tr valign='top'>
<td><font face='Verdana' size='2' >$nt[num_orders]</font></td>
<td><font face='Verdana' size='2' >$nt[date1]</font></td>
<td><font face='Verdana' size='2' >$nt[month1]</font></td>
<td><font face='Verdana' size='2' >$nt[month2]</font></td>
<td><font face='Verdana' size='2' >$nt[difference]</font></td>
</tr>";
echo "<tr bgcolor=#c2c2c2>
<td colspan=3></td>
<td><b>Month Total</b></td>
<td>$nt[daily_total]</td>
</tr>";
}
echo "<tr valign='top'><td colspan=5></td></tr>";
$query1="SELECT * FROM `sale` where date1>='2012-01-28' AND date1<='2013-01-31' and tax_name='C.S.T. 4% ON SCRAP' and tin='Applied For' and gtotal ";
$qt1=mysql_query($query1);
echo mysql_error();
$num=mysql_num_rows($qt1);
if($num)
{
$p=0;
$q=0;
while($nt1=mysql_fetch_array($qt1))
{
$p=$p+$nt1['gtotal'];
$q=$q+$nt1['subtotal'];
}
}
echo "<tr valign='top' bgcolor=#c6c6c6>
<td><font face='Verdana' size='2' >$C</font></td>
<td><font face='Verdana' size='2' ><b>subtotal<br></b></font></td>
<td><font face='Verdana' size='2' >$q</font></td>
<td><font face='Verdana' size='2' ><b>gtotal<br></b></font></td>
<td><font face='Verdana' size='2' >$p</td>
</tr>";
echo "</table>";
?>
但我有数百个数据项要计算。假设我在一月份有 5 个数据项,它们都应该在 5 行中看到,并且在月底应该有月份总数,依此类推,直到 12 个月。例如看这张图片:-我希望你能理解我的问题并能帮助我。