web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. AIGC工具
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站
    手机应用

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

565 ℃

<?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://ipkd.cn/webs_1080.html

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

当前位置: 网站首页 > 帝国cms
本文共计1412个字,预计阅读时长10分钟

基金从业资格考试题库

一站式备考基金从业资格考试,收录2021-2025年模拟题库!呱呱工具箱

AI工作站

收录全球3800+ 款各行各业AI应用,轻轻松松做事!

生活小工具

收录了万年历、老黄历、八字智能排盘等100+款小工具!生活小工具
上一篇: 推荐一个免费、无版权的古典音乐下载的网站——Musopen
下一篇: 推荐一款免费在线图片处理工具——佐糖图片平台
x 打工人ai神器