如何总计总价值

时间:2018-05-09 20:19:15

标签: php mysqli

我试图总结价值。我已经存储在数据库中的价格和数量。该特定项目的总=价格*数量。

我想总结一下子项的所有项目。

$sql = "SELECT * FROM parts WHERE jobno = '$id'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $results['entrants'][] = array(
            'partno'          => $row['partno'],
            'description'        => $row['description'],
            'price' => $row['price'],
            'qty'          => $row['qty'],
            'total' => $row['price'] * $row['qty'],
        );
    }
} else {
    // Create an empty placeholder record if no entrants were found
    $results['entrants'][] = array(
        'partno'          => NULL,
        'description'        => NULL,
        'price' => NULL,
        'qty'          => NULL,
        'total' => NULL,
    );
}

我只知道SELECT总和 - 我认为它不会起作用,因为我没有从数据库中获取信息,而是从上面获取信息。

1 个答案:

答案 0 :(得分:2)

随时跟踪:

$subTotal = 0;
while ($row = $result->fetch_assoc()) {
    $lineTotal = $row['price'] * $row['qty'];
    $subTotal += $lineTotal;
    $results['entrants'][] = array(
        //...
        'total' => $lineTotal;
    );
 }
$results['subtotal'] = $subTotal;