帝国CMS模板调用购物车商品数量和总价

572 ℃

<?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/'>结算&gt;&gt;</a> </div>";
}
?>

 

利用php语法实现一个哔哩哔哩热榜api

php删除接口:在指定的数据库数据表中根据ID删除指定的数据

php插入接口:在指定的数据库数据表中插入数据

php语法对index.php首页进行判断(根据需要显示不同页面)

html文本如何读取数据库代码示例

标签: cookie清空, php语法, 商品, 购物车

上面是“帝国CMS模板调用购物车商品数量和总价”的全面内容,想了解更多关于 帝国cms 内容,请继续关注web建站教程。

当前网址:https://m.ipkd.cn/webs_1080.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

生活小工具

收录了万年历、老黄历、八字智能排盘等100+款小工具!生活小工具
织梦dedecms 7类模板常用开发标签的写法
基于 Vue3 和 Element Plus免费开源admin前端UI框架——SCUI Admin
vue在js中封装Axios(请求拦截器,响应拦截器)完整代码
php如何将年月日相互转换时间戳(互转)
帝国CMS技巧之实现手机WAP端二级访问