我正在与一个系统项目一起工作,在这里我可以记录燃油费用,已经合并了两个不同表中一个名为“数量”的属性,并且列中的总和值已经使用javascript进行了编码,但是我需要一个功能代码,但我不知道该怎么做, 此功能显示了一份详尽的报告,如果我点击了1月,则想显示存储在1月的所有值。
这是我的html表单的构造方式:(这是我想在按钮被点击时显示模式的地方,然后从一月到二月给我一个选择)
<div class="panel-body">
<h3 align="center">Withdrawal Per Vehicle</h3>
<p id="demo"></p>
<table id="expense_table"class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Plate Number</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php foreach($Withdraw as $w): ?>
<?= '<tr>' ?>
<?= '<td class="rowDataSd">' . $w["carid"] . '</td>' ?>
<?= '<td class="rowDataSd">' . $w["reg_num"] . '</td>' ?>
<?= '<td class="rowDataSd">' . $w["amount"] . '</td>' ?>
<?= '</tr>' ?>
<?php endforeach; ?>
</tbody>
</table>
<span id="val"></span>
</div>
这是SQL语句如何汇总来自不同表的所有值:
$Withdraw = query("SELECT tbl_vehicle.carid,
tbl_vehicle.reg_num,
IFNULL(tbl_fuel.sum_amount,0) +
IFNULL(tbl_maintenance.sum_amount,0)
AS amount
FROM tbl_vehicle tbl_vehicle
LEFT JOIN
(SELECT carid,
sum(trim(replace(amount, '$', ''))+0) sum_amount
FROM tbl_fuel
GROUP BY carid) tbl_fuel ON tbl_fuel.carid = tbl_vehicle.carid
LEFT JOIN
(SELECT carid,
sum(trim(replace(amount, '$', ''))+0) sum_amount
FROM tbl_maintenance
GROUP BY carid) tbl_maintenance ON tbl_maintenance.carid = tbl_vehicle.carid
WHERE tbl_fuel.sum_amount IS NOT NULL OR tbl_maintenance.sum_amount IS NOT NULL");
这就是脚本如何构造以获取列中的总值:
<script type="text/javascript">
var table = document.getElementById("expense_table"), sumVal=0;
for(var i=1;i<table.rows.length;i++){
sumVal=sumVal+ parseFloat(table.rows[i].cells[2].innerHTML);
}
console.log(sumVal);
document.getElementById("val").innerHTML="Total Expenses: " + sumVal;
这就是我将值插入数据库表tbl_fuel的方式。
$result = $_POST["employee"];
$result_explode=explode('|', $result);
$result = query("INSERT INTO tbl_fuel
(employee_id,
carid,
account_id,
trip_date,
invoice,
up_date,
previous,
present,
liter,
amount,
purpose,
code,
attachment,
dateAdded)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
$result_explode[1],
$_POST["platenumber"],
$_POST["account_id"],
$_POST["trip_date"],
$_POST["invoice"],
$_POST["up_date"],
$_POST["previous"],
$_POST["present"],
$_POST["liter"],
$_POST["amount"],
$_POST["purpose"],
$_POST["code"],
$target_file,
date("Y-m-d"));
这是第二个SQL(我数据库中的tbl_maintenance)
$result = $_POST["employee"];
$result_explode=explode('|', $result);
$result = query("INSERT INTO tbl_maintenance
(employee_id,
carid,
account_id,
main_date,
invoice,
up_date,
previous,
present,
particular,
code,
amount,
purpose)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?)",
$result_explode[1],
$_POST["platenumber"],
$_POST["account_id"],
$_POST["main_date"],
$_POST["invoice"],
$_POST["up_date"],
$_POST["previous"],
$_POST["present"],
$_POST["particular"],
$_POST["code"],
$_POST["amount"],
$_POST["purpose"]);
如果您注意到,两个表格的龋齿和金额属性相同, 我希望它们的价值每月显示一次。这些表的值已经使用上面的第一个SQL状态合并了。