溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

帝國CMS如何調(diào)用購物車商品數(shù)量和總價(jià)

發(fā)布時(shí)間:2020-10-19 15:07:01 來源:億速云 閱讀:120 作者:小新 欄目:建站服務(wù)器

這篇文章將為大家詳細(xì)講解有關(guān)帝國CMS如何調(diào)用購物車商品數(shù)量和總價(jià),小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

代碼如下:

<?php
/**
* 根據(jù)Cookie值對(duì)購物車商品數(shù)量和總價(jià)格調(diào)用
*/
require("class/connect.php");
$totalProducts = 0; //購物車商品總數(shù)
$totalPrice = 0.0; //購物車商品總價(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; //購物車商品總價(jià)累加
$totalProducts += $thisNum; //購物車商品總數(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ù)庫鏈接不成功,請(qǐng)檢查!");
}
if(!$result = $connect -> query($query)){
throw new Exception("查詢失敗!");
}
$row = $result -> fetch_assoc();
return $row['price'];
}
/**鏈接數(shù)據(jù)庫*/
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'>您的購物車中有 {$totalProducts} 件商品,總計(jì)金額 ¥{$totalPrice}元。
點(diǎn)擊查看 <a href='/e/ShopSys/buycar/'>結(jié)算&gt;&gt;</a> </div>";
}
?>

關(guān)于帝國CMS如何調(diào)用購物車商品數(shù)量和總價(jià)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI