帝国CMS调用购物车商品数量和总价的方法
来源:本站原创|时间:2020-01-11|栏目:帝国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>";
}
?>
/**
* 根据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>";
}
?>
希望本文所述对大家的帝国CMS建站有所帮助。
栏 目:帝国cms
下一篇:帝国CMS实现高效随机调用的方法
本文标题:帝国CMS调用购物车商品数量和总价的方法
本文地址:https://www.xiuzhanwang.com/a1/diguocms/12451.html
您可能感兴趣的文章


阅读排行
本栏相关
- 01-11帝国CMS调用自定义列表名称的方法(简
- 01-11帝国CMS在自定义列表中获取当前列表
- 01-11帝国CMS 功能解密 网站安全防火墙使用
- 01-11帝国cms 顶踩 百分比 数字 完美显示解
- 01-11帝国下载系统2.5添加迅雷快车专用链
- 01-11下载内容页显示迅雷,快车加密地址标
- 01-11帝国cms 二级域名绑定栏目的最完美的
- 01-11帝国CMS扩展SQL程序编写介绍
- 01-11帝国下载系统 V2.5 增加FCKeditor编辑器
- 01-11帝国CMS常用函数介绍(二次开发参考
随机阅读
- 01-10VBS教程:函数-Year 函数
- 01-10VBS教程:属性-ShortPath 属性
- 01-10写个设置命令的VBS脚本代码
- 01-10C#异常捕获机制图文详解
- 01-10Java变量常量声明和定义原理解析
- 01-10C#制作简单的多人在线即时交流聊天室
- 01-10C++实现“隐藏实现,开放接口”的方
- 01-10利用wsc制作的一个asp后门
- 02-16织梦dedecms会员注册邮件验证设置方法
- 01-10C++中strcpy函数的实现