简体   繁体   English

按月显示总值

[英]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 04-05-2020 $50 50 美元
02-05-2020 02-05-2020 $30 30 美元
14-05-2020 14-05-2020 $10 10 美元
TOTAL全部的 $90 90 美元
Date日期 Amount数量
04-07-2020 04-07-2020 $15 15 美元
02-07-2020 02-07-2020 $15 15 美元
14-07-2020 14-07-2020 $15 15 美元
TOTAL全部的 $45 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:这也是一个很好的做法:

  1. Use 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.这样,您在轮换 ID 之前有足够的时间,最好使用一些长期保证的唯一值,例如UUIDGUID作为主键。 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.在这种情况下,如果发票编号是唯一的,最好选择 ID 作为 ID,因为它是此类记录的自然 ID。
  2. Use the smallest possible currency of the target market, this way you will avoid using decimals (which are inefficient) or worst floating point numbers (which are terrible at precision, since computers work in base 2 and humans work in base 10).使用目标市场的最小可能货币,这样您将避免使用小数(效率低下)或最差的浮点数(精度很差,因为计算机以 2 为基数工作,而人类以 10 为基数工作)。 Also, if you needed to divide any currency values (like calculating rebate and discounts), always use the same rounding policy on the smallest currency.此外,如果您需要除以任何货币价值(例如计算回扣和折扣),请始终对最小货币使用相同的舍入策略。 For example, if you are going to give a 10% discount on an item with 9.95$, you can choose the policy to round discount up (to avoid customer dissatisfaction), but stick to it all the time, also use BCD operations since they are calculating values like humans do and not like computers do, but are expensive.例如,如果您要为 9.95 美元的商品提供 10% 的折扣,您可以选择将折扣向上取整的策略(以避免客户不满意),但始终坚持下去,也使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM