帝国CMS调用购物车商品数量和总价的方法


帝国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}元。
点击查看 <a href='/e/ShopSys/buycar/'>结算&gt;&gt;</a> </div>";?
}?
?>

?


上一篇:帝国cms7.5会员注册跳过选择会员注册类型直接注册的方法

下一篇:帝国cms Table 'xx.enewsmemberadd' doesn't exist select userpic from enewsmemberadd


数量 商品 总价
Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器