温馨提醒
如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!
本文最后更新于2020年12月8日,已超过 30天没有更新
<?php /** * 根据Cookie值对购物车商品数量和总价格调用 */ require("class/connect.php"); $totalProducts = 0; //购物车商品总数 $totalPrice = 0.0; //购物车商品总价 // |77,243|2!|77,237|3! $cookieString = explode("!",$_COOKIE['zeuqcmybuycar']); try{ //遍历商品 for($i = 0; $i < count($cookieString)-1; $i++){ $priceAndNumber = explode("|",$cookieString[$i]); $thisNum = $priceAndNumber[2]; //当前商品的数量 $thisId = explode(",",$priceAndNumber[1]); $thisId = $thisId[1]; //当前商品的ID $thisPrice = this_price($thisId); //当前商品价格 $totalPrice += $thisPrice * $thisNum; //购物车商品总价累加 $totalProducts += $thisNum; //购物车商品总数累加 } echo "document.write("".displayResultJs($totalPrice,$totalProducts)."")"; //显示结果Js形式输出 }catch (Exception $e){ echo $e->getMessage(); } /**计算商品价格*/ function this_price($id){ $connect = connectDB(); $query = "select price from phome_ecms_news where id = ".$id; if(!$connect){ throw new Exception("数据库链接不成功,请检查!"); } if(!$result = $connect -> query($query)){ throw new Exception("查询失败!"); } $row = $result -> fetch_assoc(); return $row['price']; } /**链接数据库*/ function connectDB(){ global $phome_db_server,$phome_db_username,$phome_db_password,$phome_db_dbname,$phome_db_char; $connect = new mysqli($phome_db_server,$phome_db_username,$phome_db_password,$phome_db_dbname); $connect -> query("set Names ".$phome_db_char); return $connect; } /**显示结果Js*/ function displayResultJs($totalPrice,$totalProducts){ return "<div class='car'>您的购物车中有 {$totalProducts} 件商品,总计金额 ¥{$totalPrice}元。<br />点击查看 <a href='/e/ShopSys/buycar/'>结算>></a> </div>"; } ?>
版权声明:原创作品,未经允许不得转载,否则将追究法律责任。
本站资源有的自互联网收集整理,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。
本文链接:悟空资源网https://www.wkzyw.com/2771.html
许可协议:《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
本站资源有的自互联网收集整理,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。
本文链接:悟空资源网https://www.wkzyw.com/2771.html
许可协议:《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
评论0+