array_sum 计算结果查询中的所有记录,而不是单独计算每一行
array_sum counting all records in result query not each row individually
正在拔头发
我有一个计算数组中所有相关 $price 值的查询
基本上初始查询检查 table 到已完成但未开具发票的作业
第二个查询(在初始查询循环内)获取所有需要加起来的项目(这些值在另一个 table ( workshop-items ) 中找到,并根据 $item 数组值
检查
总计计算正常,我认为这与 $total 的放置位置有关,因为它将所有返回的总计相加而不是单个行的总计
下面的代码
<ul class="list-group">
<?php
$uninvoicedq = mysqli_query($con,"SELECT * FROM `workshop-jobs` WHERE completed = '1' AND invoiced = '0' AND wscid !='0' ORDER BY workstartdate ASC");
$uninvoiced = mysqli_fetch_assoc($uninvoicedq);
if($uninvoiced) {
do {
// User Query
$wscid = $uninvoiced['wscid'];
$userq = mysqli_query($cona,"SELECT * FROM `users` WHERE userid = '$wscid'");
$user = mysqli_fetch_assoc($userq);
$wtbdq = mysqli_query($con,"SELECT * FROM `workshop-jobs` WHERE wsjid = '$uninvoiced[wsjid]'");
$wtbdr = mysqli_fetch_assoc($wtbdq);
do {
$wtbd = explode(":",$wtbdr['worktobedone']);
foreach($wtbd as $item)
{
$priceq = mysqli_query($con,"SELECT * FROM `workshop-items` WHERE wsiid = '$item'");
$pricer = mysqli_fetch_assoc($priceq);
$price[] = $pricer['incvat'];
$items[] = $pricer['description'];
//echo $item.' - '. $pricer['incvat'].'<br>';
}
$total = array_sum($price);
} while($wtbdr = mysqli_fetch_assoc($wtbdq));
?>
<li class="list-group-item text-right" style="border:none;" title="<?php echo $itemview;?>"><span class="badge pull-left" style="background-color:#F00;">Not Invoiced</span><?php echo '£'.$total.' - '; echo $user['forename'].' '.$user['surname'].' - ' .$uninvoiced['summary'];?> </li>
<?
$itemList = implode(":",$items);
$itemview = str_replace(":","\n",$itemList);
?>
<? } while($uninvoiced = mysqli_fetch_assoc($uninvoicedq));
} else {
echo "No Jobs Waiting To Invoiced";
}
?>
</ul>
如果您的意思是 do while
的每一行应该是不同的总数,那么,当 do
开始时,设置 $price = [];
或 $price = array();
或 null
,因为你会将之前的所有价格加起来作为最终价格,如果你的最终价格是针对do while
的每个查询而不是针对漏洞,请按我说的去做。
记得做 $total +=
而不是 $total =
因为你会覆盖你在主循环外使用的变量,所以你会得到一个错误的总数。
正在拔头发
我有一个计算数组中所有相关 $price 值的查询 基本上初始查询检查 table 到已完成但未开具发票的作业 第二个查询(在初始查询循环内)获取所有需要加起来的项目(这些值在另一个 table ( workshop-items ) 中找到,并根据 $item 数组值
检查总计计算正常,我认为这与 $total 的放置位置有关,因为它将所有返回的总计相加而不是单个行的总计
下面的代码
<ul class="list-group">
<?php
$uninvoicedq = mysqli_query($con,"SELECT * FROM `workshop-jobs` WHERE completed = '1' AND invoiced = '0' AND wscid !='0' ORDER BY workstartdate ASC");
$uninvoiced = mysqli_fetch_assoc($uninvoicedq);
if($uninvoiced) {
do {
// User Query
$wscid = $uninvoiced['wscid'];
$userq = mysqli_query($cona,"SELECT * FROM `users` WHERE userid = '$wscid'");
$user = mysqli_fetch_assoc($userq);
$wtbdq = mysqli_query($con,"SELECT * FROM `workshop-jobs` WHERE wsjid = '$uninvoiced[wsjid]'");
$wtbdr = mysqli_fetch_assoc($wtbdq);
do {
$wtbd = explode(":",$wtbdr['worktobedone']);
foreach($wtbd as $item)
{
$priceq = mysqli_query($con,"SELECT * FROM `workshop-items` WHERE wsiid = '$item'");
$pricer = mysqli_fetch_assoc($priceq);
$price[] = $pricer['incvat'];
$items[] = $pricer['description'];
//echo $item.' - '. $pricer['incvat'].'<br>';
}
$total = array_sum($price);
} while($wtbdr = mysqli_fetch_assoc($wtbdq));
?>
<li class="list-group-item text-right" style="border:none;" title="<?php echo $itemview;?>"><span class="badge pull-left" style="background-color:#F00;">Not Invoiced</span><?php echo '£'.$total.' - '; echo $user['forename'].' '.$user['surname'].' - ' .$uninvoiced['summary'];?> </li>
<?
$itemList = implode(":",$items);
$itemview = str_replace(":","\n",$itemList);
?>
<? } while($uninvoiced = mysqli_fetch_assoc($uninvoicedq));
} else {
echo "No Jobs Waiting To Invoiced";
}
?>
</ul>
如果您的意思是 do while
的每一行应该是不同的总数,那么,当 do
开始时,设置 $price = [];
或 $price = array();
或 null
,因为你会将之前的所有价格加起来作为最终价格,如果你的最终价格是针对do while
的每个查询而不是针对漏洞,请按我说的去做。
记得做 $total +=
而不是 $total =
因为你会覆盖你在主循环外使用的变量,所以你会得到一个错误的总数。