温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

帝国CMS如何调用购物车商品数量和总价

发布时间:2020-10-19 15:07:01 来源:亿速云 阅读:116 作者:小新 栏目:建站服务器

这篇文章将为大家详细讲解有关帝国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>";
}
?>

关于帝国CMS如何调用购物车商品数量和总价就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI