求解 資料庫 一列商品的價格相加得到總價

來源:互聯網
上載者:User
這學期要做一個購物的網站,大部分都完成了,基本只剩下計算總價了

我的資料庫是一個購物車的列表通過外鍵連到我的商品列表,那裡有商品的單價,如,cart是我的購物車,product是商品列表:


然後我已經能在我的頁面上匯出我的資料了

但是我想要在下面計算出我的總價,印象中好像不是很難,但是完全記不起來了,求救,謝謝了,下面是我提取資料的代碼,最後這3個問號就是我想得到的總價:

function checkItems()
{
$result = mysql_query("SELECT * FROM cart");
$num_rows = mysql_num_rows($result);
if(!$num_rows)
{
echo '

No product have been selected
';
}
else
{
$sql = "SELECT cart.cartID, cart.productID,product.productName,product.productPrice FROM cart INNER JOIN product ON cart.productID=product.productID ORDER BY cart.cartID";
$result = mysql_query($sql);
echo ("
echo ("Cart IDProduct NamePriceRemove Item");
while($row = mysql_fetch_array($result)){
{
$id = $row['cartID'];
$proid = $row['productID'];
$proname = $row['productName'];
$price = $row['productPrice'];


echo ("$id$proname$priceRemove");
}
}
}
echo ("");
echo "The total cost is $". $???;
}



回複討論(解決方案)

$total = 0;
while($row = mysql_fetch_array($result)){
{
$id = $row['cartID'];
$proid = $row['productID'];
$proname = $row['productName'];
$price = $row['productPrice'];
$total += $price;

太感謝了!看到這個+=才想起來去年學過,完全忘了!!!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.