帝国CMS模板调用购物车商品数量和总价
时间:2024年03月07日
/来源:网络
/编辑:佚名
帝国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>";
}
?>
<?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>";
}
?>
新闻资讯 更多
- 【帝国cms教程】帝国CMS模板变量$GLOBALS[navclassid]用法分析04-03
- 【帝国cms教程】鲜为人知帝国CMS内容页调用上一篇和下一篇的精华方法汇总04-03
- 【帝国cms教程】怎么快速找出帝国CMS数据库配置文件路径及迁移网站后修改技巧!04-03
- 【帝国cms教程】帝国CMS模板$GLOBALS[navclassid]用法详解04-03
- 【帝国cms教程】帝国cms 7.5版列表页分页样式修改笔记04-02
- 【帝国cms教程】解决帝国CMS搜索页面模板不支持灵动标签和万能标签的方法04-02
- 【帝国cms教程】帝国CMS只备份栏目和模板的方法04-02
- 【帝国cms教程】帝国CMS怎样删除清空数据库记录?04-02
热门文章
- 178Moban源码谈谈免费源码与收费源码的区别
- 2帝国CMS忘记后台登陆用户名、密码、认证码的解决方法
- 3帝国CMS(EmpireCMS) v7.5后台任意代码执行漏洞及具体修复方法
- 4帝国CMS和WordPress 哪个好?哪个适合建站?
- 5如何解决Discuz的密码错误次数过多请15分钟后登陆的问题
- 6帝国cms灵动标签取得内容和栏目链接地址
- 7emlog pro 注册码“开心”教程(如果有一天,emlog官方版 或者 emlog免费版 跑路了,那用户怎么办?)
- 8织梦CMS在nginx下设置伪静态方法(附nginx伪静态规则)
- 9帝国cms后台登录出现”您还未登录”怎么解决?
- 10帝国cms7.5忘记登录密码 和忘记登录认证码以及多次登录 失败被锁定解决办法