如何才能显示所有分区明显的总分?
示例:
我想要这样的数据
---------------------------------------------------------------
CARD NO NAME CAR NO DIVISION DATE AMOUNT
---------------------------------------------------------------
80351 MANWATERTANKER 4454HM 102 2013-9-30 95.000
60759 TIPPER 2368BK 102 2013-8-31 77.500
64545 MFASIL 3334HG 102 2013-9-30 20.000
56565 XYAZ 93F68K 102 2013-8-31 55.000
---------------------------------------------------------------
Total 247.500
---------------------------------------------------------------
CARD NO NAME CAR NO DIVISION DATE AMOUNT
---------------------------------------------------------------
90178 ATEEQ 8931RR 105 2013-7-31 551.000
43325 USMAN 6732YY 105 2013-6-30 12.500
55598 ALAAM 4631TT 105 2013-7-31 158.000
---------------------------------------------------------------
Total 721.500
---------------------------------------------------------------
CARD NO NAME CAR NO DIVISION DATE AMOUNT
---------------------------------------------------------------
40678 FATHISALIM 1632AM 103 2013-6-30 454.440
---------------------------------------------------------------
Total 454.440
-----------------------------------------------------------------
AND OVERALL TOTAL 1423.440
-----------------------------------------------------------------
现在它显示如下
---------------------------------------------------------------
CARD NO NAME CAR NO DIVISION DATE AMOUNT
---------------------------------------------------------------
80351 MANWATERTANKER 4454HM 102 2013-9-30 95.000
60759 TIPPER 2368BK 102 2013-8-31 77.500
40678 FATHISALIM 1632AM 103 2013-6-30 454.440
90178 ATEEQ 8931RR 105 2013-7-31 551.000
64545 MFASIL 3334HG 102 2013-9-30 20.000
56565 XYAZ 93F68K 102 2013-8-31 55.000
43325 USMAN 6732YY 105 2013-6-30 12.500
55598 ALAAM 4631TT 105 2013-7-31 158.000
---------------------------------------------------------------
Total 1423.440
这是我的代码:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("car", $con);
function formatMoney($number, $fractional=false) {
if ($fractional) {
$number = sprintf('%.2f', $number);
}
while (true) {
$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
if ($replaced != $number) {
$number = $replaced;
} else {
break;
}
}
return $number;
}
$a=$_POST['from'];
$b=$_POST['to'];
echo "<span align='center' class='style2'>Report For The Period Of $a to $b</span>";
echo "<div id='non-printable'><table class='hovertable' border='1' cellpadding='10'>";
echo "<tr> <th>CardNo</th> <th>NAME/th><th>CARNO</th><th>Division</th> <th>Date</th><th>AMOUNT</th></tr>";
// get results1 from database
$result1 = mysql_query("SELECT * FROM fuel where date BETWEEN '$a' AND '$b' order by division ASC");
while($row = mysql_fetch_array($result1))
{
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['cardno'] . '</td>';
echo '<td>' . $row['drivernamefuel'] . '</td>';
echo '<td>' . $row['carno'] . '</td>';
echo '<td>' . $row['division'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['amount'] . '</td>';
echo "</tr>";
//Increment the value of the Total_total variable
//by the salary value of one row till the while loop finishes
$Total_amount=$Total_amount+$row['amount'];
}
echo "<tr>";
echo '<td>Total</td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '<td>' . $Total_amount .'</td>';
echo "</tr>";
// close table>
echo "</table>";
mysql_close($con);
?>
答案 0 :(得分:1)
只需更改代码的这一部分......
echo "<span align='center' class='style2'>Report For The Period Of $a to $b</span>";
echo "<div id='non-printable'><table class='hovertable' border='1' cellpadding='10'>";
echo "<tr> <th>CardNo</th> <th>NAME/th><th>CARNO</th><th>Division</th> <th>Date</th><th>AMOUNT</th></tr>";
// get results1 from database
$currentdiv = '';
$total_amount = 0.00;
$all_total_amount = 0.00;
$result1 = mysql_query("SELECT * FROM fuel where monthdate BETWEEN '$a' AND '$b' order by division ASC");
while($row = mysql_fetch_array($result1)) {
if(trim(strtolower($row['division'])) != $currentdiv) {
echo "<tr>";
echo '<td>Total</td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
sprintf('<td>%.2f</td>', $total_amount);
echo "</tr>";
echo "<tr></tr>";
echo "<tr><th>CardNo</th> <th>NAME/th><th>CARNO</th><th>Division</th> <th>Date</th><th>AMOUNT</th></tr>";
$all_total_amount += $total_amount;
$total_amount = 0.00;
$currentdiv = trim(strtolower($row['division']));
}
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['cardno'] . '</td>';
echo '<td>' . $row['drivernamefuel'] . '</td>';
echo '<td>' . $row['carno'] . '</td>';
echo '<td>' . $row['division'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
sprintf('<td>%.2f</td>', floatval($row['amount']));
echo "</tr>";
$total_amount += floatval($row['amount']);
}
echo "<tr>";
echo '<td>Total</td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
sprintf('<td>%.2f</td>', $total_amount);
echo "</tr>";
$all_total_amount += $total_amount;
echo "<tr>";
echo '<td>All Total</td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
sprintf('<td>%.2f</td>', $all_total_amount);
echo "</tr>";
// close table>
echo "</table>";
答案 1 :(得分:0)
您可以尝试这样的事情:
echo "<div id='non-printable'><table class='hovertable' border='1' cellpadding='10'>";
echo "<tr><th>CardNo</th> <th>NAME/th><th>CARNO</th><th>Division</th> <th>Date</th><th>AMOUNT</th></tr>";
// get results1 from database
$result1 = mysql_query("SELECT * FROM fuel where date BETWEEN '$a' AND '$b' order by division ASC");
$division = null;
$division_total = 0;
while($row = mysql_fetch_array($result1))
{
if ($division !== null && $row['division'] != $division)
{
echo "<tr>";
echo '<td>Total</td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '<td>' . $division_total .'</td>';
echo "</tr>";
echo "</table>";
echo "<table class='hovertable' border='1' cellpadding='10'>";
echo "<tr><th>CardNo</th> <th>NAME</th><th>CARNO</th><th>Division</th> <th>Date</th><th>AMOUNT</th></tr>";
$division_total = 0;
}
$division = $row['division'];
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['cardno'] . '</td>';
echo '<td>' . $row['drivernamefuel'] . '</td>';
echo '<td>' . $row['carno'] . '</td>';
echo '<td>' . $row['division'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['amount'] . '</td>';
echo "</tr>";
//Increment the value of the Total_amount variable
//by the salary value of one row till the while loop finishes
$division_total += $row['amount'];
$Total_amount=$Total_amount+$row['amount'];
}
echo "<tr>";
echo '<td>Total</td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '<td>' . $division_total .'</td>';
echo "</tr>";
echo "</table>";
echo "<table class='hovertable' border='1' cellpadding='10'>";
echo "<tr><th>AND OVERALL TOTAL</th> <th></th><th></th><th></th> <th></th><th>". $Total_amount ."</th></tr>";
echo "</table>";
echo "</div>";
另外,我建议您清理查询。至少使用mysql_real_escape_string()。 e.g。
$result1 = mysql_query("SELECT * FROM fuel where date BETWEEN '".mysql_real_escape_string($a)."' AND '".mysql_real_escape_string($b)."' order by division ASC");
然而,绑定您的参数更安全。 http://bobby-tables.com/php.html
希望这有帮助!