简体   繁体   English

创建一个在线购物车

[英]Creating an Online Shopping Cart

I am trying to sum the price of all products selected. 我想对所有选定产品的价格求和。 If I chose 2 Products for example, whose prices are 13 and 15 then the output will be : "13 15" 例如,如果我选择2个产品,其价格分别为13和15,则输出将是:“ 13 15”

My question is, How can I output the result of the two items? 我的问题是,如何输出两个项目的结果? ( for this example , 28 ) (对于此示例,为28)

for ($pos=0; $pos<=$level2; $pos++) { 
    $level3 = $level1[$pos];
    echo " ";
    $result2 = mysql_query("SELECT * FROM products WHERE id='$level3'");
    $results2 = array();
    while($row2 = mysql_fetch_array($result2))
    {
        $results2[] = $row2['price'];
        $level4 = array_sum($results2);
        echo "Price is".$level4;
    }

I would do it like this: 我会这样做:

$sum = 0;
for ($pos=0; $pos<=$level2; $pos++) { 
    $level3 = $level1[$pos];
    $result2 = mysql_query("SELECT * FROM products WHERE id='$level3'");
    while($row2 = mysql_fetch_array($result2))
    {
        $sum += $row2['price'];
    }
}
echo 'Price is '.$sum;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM