计算mysqli/php生成的数量

qij5mzcb  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(332)

我有一个表单,其中我给出了所选发票的开始和结束日期,并用这些发票的数据生成一个xml。
我想在这个xml的末尾计算一些金额和生成的发票数量。
例如:我选择01.12.2018作为发票的开始日期,31.12.2018作为发票的结束日期。我的函数从mysql数据库中提取有关7张发票的信息,这些发票都在这些日期。。例如,数据是每张发票的金额。我希望在xml文件的末尾有一个已提取发票的摘要—编号和所有生成发票的金额。
我的代码:

$header = $dom->createElement('Header');
for($i=1; $i<count($array); $i++) {
   $arrayAmount = $array[$i]['amount'];
   $invoice = $dom->createElement('Invoice');
   $invoice->appendChild($CountAmount   = $dom-> 
   createElement('AmountInvoice', $arrayAmount) );
   $allInvoices = $dom->createElement('AllInvoices');
   $allInvoices->appendChild($allInvoices = $dom-> 
   createElement('AllAmountInvoices',**$amountInvoices**) );
   $allInvoices->appendChild($allInvoices = $dom-> 
   createElement('NumberOfInvoices',**$numberOfInvoices**) );
}

带星号的变量是空格,我的函数将显示发票的金额和数量。
我试着这样做:

$amountInvoicesSQL = mysqli_query($mysqli, "SELECT SUM(amount) FROM INVOICES WHERE issue_date BETWEEN ? AND ?");
$amountInvoicesMYSQL = mysqli_fetch_assoc($amountInvoicesSQL);
$amountInvoices = $amountInvoicesMYSQL['amount'];

你知道吗?:)

dphi5xsq

dphi5xsq1#

您可以很容易地保持总金额和发票的数量只是相同的控制在您的帐户 for 循环。。。

$header = $dom->createElement('Header');
$total = 0;
for($i=1; $i<count($array); $i++) {
    $arrayAmount = $array[$i]['amount'];
    $invoice = $dom->createElement('Invoice');
    $invoice->appendChild( $dom-> createElement('AmountInvoice', $arrayAmount) );
    $total += $arrayAmount;
}

$allInvoices = $dom->createElement('AllInvoices');
$allInvoices->appendChild( $dom-> createElement('AllAmountInvoices', $total ));
$allInvoices->appendChild( $dom-> createElement('NumberOfInvoices', count($array) ));

你不应该把总数加到每个循环中,只在循环结束时加一次。但我不知道你把这些加到哪里去了 $header 完全。
还要注意,我已经删除了创建元素时为变量赋值的方式

$allInvoices->appendChild($allInvoices = $dom-> 
       createElement('NumberOfInvoices',**$numberOfInvoices**) );

变成

$allInvoices->appendChild($dom-> createElement('NumberOfInvoices',**$numberOfInvoices**) );

相关问题