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

本文实例讲述了帝国cms调用购物车商品数量和总价的方法。分享给大家供大家参考。具体实现方法如下:

代码如下:

<?php /**
* 根据Cookie值对购物车商品数量和总价格调用
*/
require("class/connect.php");
$totalProducts = 0; //购物车商品总数
$totalPrice = 0.0; //购物车商品总价
// |77,243|2!|77,237|3!
$cookieString = explode("!",$_COOKIE[&#39;zeuqcmybuycar&#39;]);
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 -&gt; query($query)){
throw new Exception("查询失败!");
}
$row = $result -&gt; 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 -&gt; query("set Names ".$phome_db_char);
return $connect;
}
/**显示结果Js*/
function displayResultJs($totalPrice,$totalProducts){
return "<div>您的购物车中有 {$totalProducts} 件商品,总计金额 ¥{$totalPrice}元。
点击查看 <a>结算&gt;&gt;</a> </div>";
}
?&gt;
登录后复制

推荐学习《帝国cms教程》

希望本文所述对大家的帝国CMS建站有所帮助。

以上就是深入帝国CMS调用购物车商品数量和总价的方法的详细内容,更多请关注慧达AI工具网其它相关文章!

转载请说明出处 内容投诉内容投诉
慧达seo-站长工具-seo工具-采集-发布-AI文章生成发布工具 » 深入帝国CMS调用购物车商品数量和总价的方法

慧达AI专注站群seo管理工具

查看演示 官网购买