[英]Shopping cart calculating total
Good day, below is a piece of code that increments the total cost for each product in the session array. 美好的一天,下面是一段代码,可以增加会话数组中每种产品的总成本。 My problem is displaying 0 at the end of the total when the last peny = 0. 我的问题是,当最后一个peny = 0时,总数的末尾显示0。
Example 1, 2.20 + 2.20 = 4.40 but only 4.4 is shown 示例1,2.20 + 2.20 = 4.40,但仅显示4.4
Example 2, 2.20 + 2.25 = 4.45 and 4.45 is shown 示例2显示了2.20 + 2.25 = 4.45和4.45
$total = 0;
if (isset($_SESSION['cartItems'])){
foreach ($_SESSION['cartItems'] as $product){
$total += $product['cost'];
}
}
echo $total;
Any advice on how to show/include when a 0 is entered? 关于输入0时如何显示/包含的任何建议?
This was already answered in a comment by WebCode.ie but here is a detailed answer that may help those facing the same problem: WebCode.ie的评论已经回答了这个问题,但是这里有一个详细的答案,可以帮助面临相同问题的人们:
To customize a number format, you can use the PHP Function string number_format()
, that accepts either one , two , or four parameters , this way: 要自定义数字格式,可以使用PHP函数string number_format()
,该string number_format()
可以通过以下方式接受一个 , 两个或四个 参数 :
$my_number=25200;
$number_decimals=2;
$dec_separator=".";
$thousands_separator=" ";
echo number_format($my_number , $number_decimals, $dec_separator, $thousands_separator);
// This will output 25 200.00
Please also note that you can only use 1, 2 or 4 parameters. 另请注意,您只能使用1、2或4个参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.