gpt4 book ai didi

PHP MySQL 双 While 循环给我带来问题

转载 作者:行者123 更新时间:2023-11-29 07:02:45 25 4
gpt4 key购买 nike

我目前有一段代码,我想基本上提取产品数量和价格,然后给出总计

我遇到的问题是数学离题很远......它本质上是在循环期间将最后一个数量乘以该商品的最后一个价格(我希望我至少解释了半盎司,哈哈!)

无论如何,这是代码,我认为它是正确的,但我在某个地方出错了,谢谢!

<table>
<tr>
<th>Product Title</th>
<th>Nicotine Strength</th>
<th>Quantity</th>
<th>Price (inc. VAT)</th>
</tr>
<?php
$query = "SELECT `product`, `variant`, `quantity` FROM orders_detail WHERE order_id = '$orderid'";
$result = mysqli_query($con, $query);
$quantitytotal = 0;
while ($row = mysqli_fetch_assoc($result)) {
$product = $row['product'];
$stuff = $row['quantity'];
$variant = $row['variant'];
$linequantity = $stuff;
$quantitytotal += $stuff;


$pricequery = "SELECT product_price FROM products WHERE product_name = '$product'";
$priceresult = mysqli_query($con, $pricequery);
$pricetag = 0;
$priceline = 0;
while ($rowprice = mysqli_fetch_assoc($priceresult)) {
$price = $rowprice['product_price'];
$priceline = $price;
$pricetag += $price;

}
echo "Price: $pricetag<br>";
echo "Quantity: $quantitytotal<br>";
$linetotal = $priceline * $linequantity;
//echo "$product - $linequantity - $linetotal<br>";
echo '<tr><td>' . $product .' </td> ' . '<td>' . $variant . '</td>' . ' <td> ' . $linequantity . '</td>' . '<td> £' . $linetotal . '</td> </tr>';

}
$total = $pricetag * $quantitytotal;
?>



<tr><td>Total Ex Vat:</td><td> Total Inc Vat:</td></tr>

<tr><td><?php echo "£" . ($total / 1.2);?></td>
<td><?php echo "£" . $total; ?></td></tr>
</table>

最佳答案

您可以通过在循环中添加$linetotal来计算总计。

$total=$total+$linetotal;

在循环之前用 0 初始化 $total

关于PHP MySQL 双 While 循环给我带来问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42791286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com