本文實(shí)例講述了帝國(guó)CMS調(diào)用購(gòu)物車(chē)商品數(shù)量和總價(jià)的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
復(fù)制代碼
代碼如下:<?php
/**
* 根據(jù)Cookie值對(duì)購(gòu)物車(chē)商品數(shù)量和總價(jià)格調(diào)用
*/
require("class/connect.php");
$totalProducts = 0; //購(gòu)物車(chē)商品總數(shù)
$totalPrice = 0.0; //購(gòu)物車(chē)商品總價(jià)
// |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]; //當(dāng)前商品的數(shù)量
$thisId = explode(",",$priceAndNumber[1]);
$thisId = $thisId[1]; //當(dāng)前商品的ID
$thisPrice = this_price($thisId); //當(dāng)前商品價(jià)格
$totalPrice += $thisPrice * $thisNum; //購(gòu)物車(chē)商品總價(jià)累加
$totalProducts += $thisNum; //購(gòu)物車(chē)商品總數(shù)累加
}
echo "document.write(\"".displayResultJs($totalPrice,$totalProducts)."\")"; //顯示結(jié)果Js形式輸出
}catch (Exception $e){
echo $e->getMessage();
}
/**計(jì)算商品價(jià)格*/
function this_price($id){
$connect = connectDB();
$query = "select price from phome_ecms_news where id = ".$id;
if(!$connect){
throw new Exception("數(shù)據(jù)庫(kù)鏈接不成功,請(qǐng)檢查!");
}
if(!$result = $connect -> query($query)){
throw new Exception("查詢(xún)失敗!");
}
$row = $result -> fetch_assoc();
return $row['price'];
}
/**鏈接數(shù)據(jù)庫(kù)*/
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;
}
/**顯示結(jié)果Js*/
function displayResultJs($totalPrice,$totalProducts){
return "<div class='car'>您的購(gòu)物車(chē)中有 {$totalProducts} 件商品,總計(jì)金額 ¥{$totalPrice}元。
點(diǎn)擊查看 <a href='/e/ShopSys/buycar/'>結(jié)算>></a> </div>";
}
?>
/**
* 根據(jù)Cookie值對(duì)購(gòu)物車(chē)商品數(shù)量和總價(jià)格調(diào)用
*/
require("class/connect.php");
$totalProducts = 0; //購(gòu)物車(chē)商品總數(shù)
$totalPrice = 0.0; //購(gòu)物車(chē)商品總價(jià)
// |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]; //當(dāng)前商品的數(shù)量
$thisId = explode(",",$priceAndNumber[1]);
$thisId = $thisId[1]; //當(dāng)前商品的ID
$thisPrice = this_price($thisId); //當(dāng)前商品價(jià)格
$totalPrice += $thisPrice * $thisNum; //購(gòu)物車(chē)商品總價(jià)累加
$totalProducts += $thisNum; //購(gòu)物車(chē)商品總數(shù)累加
}
echo "document.write(\"".displayResultJs($totalPrice,$totalProducts)."\")"; //顯示結(jié)果Js形式輸出
}catch (Exception $e){
echo $e->getMessage();
}
/**計(jì)算商品價(jià)格*/
function this_price($id){
$connect = connectDB();
$query = "select price from phome_ecms_news where id = ".$id;
if(!$connect){
throw new Exception("數(shù)據(jù)庫(kù)鏈接不成功,請(qǐng)檢查!");
}
if(!$result = $connect -> query($query)){
throw new Exception("查詢(xún)失敗!");
}
$row = $result -> fetch_assoc();
return $row['price'];
}
/**鏈接數(shù)據(jù)庫(kù)*/
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;
}
/**顯示結(jié)果Js*/
function displayResultJs($totalPrice,$totalProducts){
return "<div class='car'>您的購(gòu)物車(chē)中有 {$totalProducts} 件商品,總計(jì)金額 ¥{$totalPrice}元。
點(diǎn)擊查看 <a href='/e/ShopSys/buycar/'>結(jié)算>></a> </div>";
}
?>
希望本文所述對(duì)大家的帝國(guó)CMS建站有所幫助。
【網(wǎng)站聲明】本站除付費(fèi)源碼經(jīng)過(guò)測(cè)試外,其他素材未做測(cè)試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請(qǐng)勿用于商業(yè)用途。如損害你的權(quán)益請(qǐng)聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。