[英]Show total of values by month
I have a simple table that shows invoices and the date they were created.
我有一个简单的表格,显示发票及其创建日期。 I am looking for a way to show the total of the invoices values at the bottom of the last row of every month while showing the table.
我正在寻找一种在显示表格时在每个月的最后一行底部显示总发票值的方法。
Example:例子:
Date![]() |
Amount![]() |
---|---|
04-05-2020 ![]() |
$50 ![]() |
02-05-2020 ![]() |
$30 ![]() |
14-05-2020 ![]() |
$10 ![]() |
TOTAL![]() |
$90 ![]() |
Date![]() |
Amount![]() |
---|---|
04-07-2020 ![]() |
$15 ![]() |
02-07-2020 ![]() |
$15 ![]() |
14-07-2020 ![]() |
$15 ![]() |
TOTAL![]() |
$45 ![]() |
Any idea?任何想法? This is what I have so far:
这是我到目前为止所拥有的:
<!DOCTYPE html>
<html>
<head>
<title>FACTURAS</title>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$server = "localhost";
$user = "root";
$password = "";
$db = "facturas";
$conn = new mysqli($server, $user, $password, $db);
if ($conn->connect_error) {
die("CONNECTION ERROR " . $conn->connect_error);
}
$sql = "SELECT * FROM facturas_table ORDER BY id";
$facturas = $conn->query($sql);
echo '<table>
<thead>
<tr>
<td>ID</td>
<td>DATE</td>
<td>INVOICE</td>
<td>PRICE</td>
</tr>
</thead>';
while ($row = $facturas->fetch_assoc()) {
$month = substr($row['fecha'], 3);
// $totalMonths = "SELECT SUM(monto) as montoTotal FROM facturas_table WHERE fecha = ''"
echo '
<tbody>
<tr>
<td>'.$row['id'].'</td>
<td>'.$row['fecha'].'</td>
<td>'.$row['factura'].'</td>
<td>'.$row['monto'].'</td>
</tr>
</tbody>';
}
echo '</table>';
?>
</body>
</html>
First of all try to divide html from php if anyhow possible, as the mix will create problems in the future.首先,尽可能尝试将 html 与 php 分开,因为混合会在未来产生问题。 Also try to use english language for all variable names (I assume monto is the price).
还尝试对所有变量名称使用英语(我假设 monto 是价格)。
What I got from your question is, that you try to create the table and also calculate the total.我从您的问题中得到的是,您尝试创建表格并计算总数。 Therefor you would create a variable for the sum and add the current amount of each row to the sum variable (in each iteration of the loop).
因此,您将为总和创建一个变量,并将每行的当前数量添加到总和变量(在循环的每次迭代中)。
I changed the relevant php part:我更改了相关的 php 部分:
UPDATED to your comment (sorry this was not so clear from your question).已更新您的评论(抱歉,您的问题不太清楚)。
Important is the trick with $lastMonth
and $month
, as well as with $first
重要的是
$lastMonth
和$month
以及$first
的技巧
<tbody>
<?php
$total = 0; $lastMonth = ""; $first = true;
while ($row = $facturas->fetch_assoc()) :
$month = substr($row['fecha'], 3);
$total += $row['monto']; ?>
<?php if(!$first && $lastMonth != $month): ?>
<tr>
<th colspan="2">Total (<?=$month?>): </th>
<th colspan="2"><?=$total?></th>
</tr>
<?php $total=0; endif; ?>
<tr>
<td><?=$row['id']?></td>
<td><?=$row['fecha']?></td>
<td><?=$row['factura']?></td>
<td><?=$row['monto']?></td>
</tr>
<?php $first=false; $lastMonth = $month; endwhile; ?>
<tr>
<th colspan="2">Total (<?=$month?>): </th>
<th colspan="2"><?=$total?></th>
</tr>
</tbody>
</table>
didn't try out the code (so be sure to check for syntax), but you can get the idea how to do that pretty well没有尝试过代码(所以一定要检查语法),但你可以知道如何很好地做到这一点
while @helle's answer focuses on a solution on PHP side, here's an answer on the database side:虽然@helle 的答案侧重于 PHP 方面的解决方案,但这是数据库方面的答案:
create table `facturas_table`(`id` bigint primary key, `invoice` varchar(100), `price` bigint, `date` date);
insert into `facturas_table` (`id`, `invoice`, `price`, `date`) values
(1, "A25C45", 5000, str_to_date("04-05-2020", "%d-%m-%Y")),
(2, "A25C46", 3000, str_to_date("02-05-2020", "%d-%m-%Y")),
(3, "A25C47", 1000, str_to_date("14-05-2020", "%d-%m-%Y")),
(4, "A25C48", 1500, str_to_date("04-07-2020", "%d-%m-%Y")),
(5, "A25C49", 1500, str_to_date("02-07-2020", "%d-%m-%Y")),
(6, "A25C50", 1500, str_to_date("14-07-2020", "%d-%m-%Y"))
;
select * from (
select `id`, `invoice`, `price`, date_format(`date`, "%d-%m-%Y") `date`, date_format(`date`, "%Y-%m-%d") `ordering_format` from `facturas_table`
union all
select null `id`, null `invoice`, `price`, `month` `date`, date_format(str_to_date(concat("01-", `month`), "%d-%m-%Y"), "%Y-%m-32") `ordering_format` from (
select sum(`price`) `price`, date_format(`date`, "%m-%Y") `month` from `facturas_table` group by `month`
) `totals`
) `a` order by `ordering_format` asc;
From there on从那里开始
<-- Some Code -->
<table>
<thead>
<tr>
<th>ID</th>
<th>DATE</th>
<th>INVOICE</th>
<th>PRICE</th>
</tr>
</thead>
<tbody>
<?php while($row = $facturas->fetch_assoc()): ?>
<?php if ($row["invoice"] === null): ?>
<tr style="font-size: bolder">
<td colspan="2">Total <?php echo $row["date"]; ?></td>
<td colspan="2"><?php echo $row["price"]; ?></td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>ID</th>
<th>DATE</th>
<th>INVOICE</th>
<th>PRICE</th>
</tr>
</thead>
<tbody>
<?php else: ?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["date"]; ?></td>
<td><?php echo $row["invoice"]; ?></td>
<td><?php echo $row["price"]; ?></td>
</tr>
<?php endif; ?>
<?php endwhile; ?>
<-- Rest of Code -->
Note that since date
is a reserved keyword in MySQL, it is wrapped in backticks (`).请注意,由于
date
是 MySQL 中的保留关键字,因此它包含在反引号 (`) 中。 It is good practice to wrap any user-defined names (even if they are not reserved) in backticks to avoid confusion and unnecessary and hard-to-catch syntactic bugs.将任何用户定义的名称(即使它们未保留)包装在反引号中是一种很好的做法,以避免混淆和不必要且难以捕捉的语法错误。
Also Note that price is written in cents and its type is bigint
.另请注意,价格以美分表示,其类型为
bigint
。 It's also good practice that:这也是一个很好的做法:
bigint
for integer primary keys on tables that are going to be massive in the long run and most probably getting archived in a warehouse in the future, too.bigint
用于 integer 表上的主键,从长远来看,这些表将是巨大的,并且很可能在未来也会被归档到仓库中。 That way you have plenty of time before rotating IDs, better still, use some long guaranteed unique values like UUID
s and GUID
s as primary key.UUID
和GUID
作为主键。 In this case, if invoice numbers are going to be unique, it's best to be chosen as ID since it's the natural ID of this kind of records.BCD
operations since they are calculating values like humans do and not like computers do, but are expensive.BCD
操作,因为它们正在像人类一样计算价值,而不是像计算机那样计算价值,但价格昂贵。 Use them in sensitive error prone situations like this (not just adding simple integers, but divisions and multiplies)$price = 995;
$discount = 10;
$discounted_value = explode(".", bcdiv(bcmul($price, $discount, 2), "100", 2), 2);
$discounted_value = $discounted_value[0] + (($discounted_value[1] + 0) !== 0);
$final_price = $price - $discounted_value;
Finally, if you think you are going to deal prices greater than PHP_INT_MAX
(take care that it's different in 32-bit and 64-bit versions), just go full berserk mode and use strings in combination of bcmath
operations for prices.最后,如果您认为您将处理高于
PHP_INT_MAX
的价格(请注意它在 32 位和 64 位版本中有所不同),只需 go 完全狂暴模式并使用字符串结合bcmath
操作来获取价格。
Having a good online business!拥有良好的在线业务!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.