3

好的,我有点卡住了,我知道我在这里缺少一件简单的事情,所以希望新鲜的眼睛会有所帮助

我有一列中的值存储为 2:7:99 等,每个值由以下分隔:

现在我可以将所有值分开并查询另一个表以获取与该值对应的价格。

我遇到的问题是对所有价格值进行求和 请参阅下面的代码我认为最简单的方法是将所有价格值添加到数组中,然后执行 array_sum() 但由于某种原因我无法让它工作

** 请不要提及 SQL 注入 .. 它在没有外部访问权限的本地机器上,只有我自己会使用它

    <?php
include('config.php');
// Function for calculation Rough Invoice Total
function basicTotal() {
    $con = mysqli_connect("localhost","USER","PASS","TABLE");
    $wtbdq = mysqli_query($con,"SELECT * FROM `jobs` WHERE wsjid = '18'");
    $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 = array($pricer['incvat']);

            echo $item.' - '. $pricer['incvat'].'<br>';

        }

    } while($wtbdr = mysqli_fetch_assoc($wtbdq));

    $total = array_sum($price);
    echo $total;
}
basicTotal();
?>

提前致谢

4

3 回答 3

4
just replace  $price = $pricer['incvat']; 

with this in your code

$price[] = $pricer['incvat'];
于 2017-05-17T12:46:08.030 回答
2

您当前方法的问题是您正在覆盖$price变量。您需要推送$price数组中的值,例如$price[] = $pricer['incvat']. 你需要这样做

    <?php
include('config.php');
// Function for calculation Rough Invoice Total
function basicTotal() {
    $con = mysqli_connect("localhost","USER","PASS","TABLE");
    $wtbdq = mysqli_query($con,"SELECT * FROM `jobs` WHERE wsjid = '18'");
    $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'];

            echo $item.' - '. $pricer['incvat'].'<br>';

        }

    } while($wtbdr = mysqli_fetch_assoc($wtbdq));

    $total = array_sum($price);
    echo $total;
}
basicTotal();
?>
于 2017-05-17T12:43:03.230 回答
2

你一直在覆盖你的最终价格:

$price = array($pricer['incvat']);

将其替换为:

$price[] = $pricer['incvat'];
于 2017-05-17T12:43:39.427 回答