帝国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/'>结算>></a> </div>";
}
?>
声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至201542412@QQ.com举报,一经查实,本站将立刻删除。
相关推荐
-
帝国cms灵动标签调用最新专题内容和专题数量的教程
帝国cms用灵动标签输出4个最新专题内容包括专题下的文章数量
-
帝国cms调用热门顶赞文章和图片数量的方法
帝国cms调用热门顶的文章和文章里面的图片数量的方法。。
-
帝国cms灵动标签调用热门tags标签包括tags的数量
用帝国cms灵动标签调用热门gs标签包括gs的数量num为标签的数量。
-
帝国cms统计某一个数据表的文章信息数量的方法
下面是统计nws表的24发布文章收录和一个月的更新数量代码如下:
-
帝国cms灵动标签查询结果实际条数数量
帝国cms灵动标签查询结果实际条数数量