帝国CMS怎么调用购物车商品数量
发布时间:2022-05-01作者:帝国建站网
帝国cms模板调用购物车商品数量和总价的方法,一起来看看吧:
代码如下:
<?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>"; } ?>
相关帝国教程
- 07-31帝国CMS模板里面灵动标签怎么调用日期时间
- 07-06帝国CMS收藏删除功能方法
- 10-26帝国cms内容页图片自适应大小方法,图片过大如何解决?
- 03-16帝国CMS设置后台友情链接每页显示数量
- 07-16修改帝国CMS的系统提示页模板
- 09-23帝国CMS获取当前页面顶级栏目名称和链接
- 05-29在帝国CMS内容页模板里调用附属专题
- 08-29帝国cms灵动标签随机专题调用方法和代码是什么
- 11-29帝国CMS搜索结果关键字高亮的修改方法
- 09-10帝国CMS模板前台使用灵动标签调用管理信息反馈中的内容
- 07-22帝国CMS自定义投稿表单教程
- 04-05帝国CMS任意页面调用指定信息的图集内容
- 11-11帝国cms会员控制中心任意地方调用会员头像
- 10-25帝国CMS系统报错:Fatal error: Maximum execution time of...
- 03-25帝国CMS采集规则针对内容newstext过滤广告正则
- 06-30帝国CMS如何提高缩略图质量
- 04-01帝国CMS使用esub/substr截取前面(或后面)两个字符(或多个字符)
- 03-15帝国CMS会员中心模板改造之调用我提交的信息反馈列表
- 05-29帝国CMS在下载内容页downpath字段显示下载点数
- 10-27帝国cms灵动标签调用上一章和下一章文章名称