Solve a database The price of a list of items is added to get the total

Source: Internet
Author: User
This semester to make a shopping site, most of them are finished, basically only the total price of the calculation

My database is a list of shopping carts that are connected to my product list by a foreign key, where there is the unit price of the item, for example, the cart is my shopping cart, product is the product list:


Then I can already export my data on my page.

But I want to calculate my total price below, the impression seems not very difficult, but can not remember, help, thank you, here is my code to extract data, and finally these 3 question marks are I want to get the total price:

function Checkitems ()
{
$result = mysql_query ("SELECT * from cart");
$num _rows = mysql_num_rows ($result);
if (! $num _rows)
{
Echo '

No product has been selected
';
}
Else
{
$sql = "Select Cart.cartid, Cart.productid,product.productname,product.productprice from the 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 $ ". $???;
}



Reply to discussion (solution)

$total = 0;
while ($row = Mysql_fetch_array ($result)) {
{
$id = $row [' Cartid '];
$proid = $row [' ProductID '];
$proname = $row [' ProductName '];
$price = $row [' Productprice '];
$total + = $price;

Thanks a lot! See this + = Just remembered to have learned last year, completely forget!!!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.