子房 發表於 2023-8-9 00:00:00

帝国CMS调用购物车商品数量和总价的方法

<p>
本文实例讲述了帝国CMS调用购物车商品数量和总价的方法。分享给大家供大家参考。具体实现方法如下:</p>
<p>
 </p>
<div>

<p>
代码如下:</p>
</div>
<div id="phpcode3">
&lt;?php <br>
/** <br>
* 根据Cookie值对购物车商品数量和总价格调用 <br>
*/ <br>
require("class/connect.php"); <br><br>
$totalProducts = 0; //购物车商品总数 <br>
$totalPrice = 0.0; //购物车商品总价 <br><br>
// |77,243|2!|77,237|3! <br>
$cookieString = explode("!",$_COOKIE['zeuqcmybuycar']); <br><br>
try{ <br>
//遍历商品 <br>
for($i = 0; $i &lt; count($cookieString)-1; $i++){ <br>
$priceAndNumber = explode("|",$cookieString[$i]); <br>
$thisNum = $priceAndNumber; //当前商品的数量 <br>
$thisId = explode(",",$priceAndNumber); <br>
$thisId = $thisId; //当前商品的ID <br>
$thisPrice = this_price($thisId); //当前商品价格 <br><br>
$totalPrice += $thisPrice * $thisNum; //购物车商品总价累加 <br>
$totalProducts += $thisNum; //购物车商品总数累加 <br>
} <br><br>
echo "document.write(\"".displayResultJs($totalPrice,$totalProducts)."\")"; //显示结果Js形式输出 <br><br>
}catch (Exception $e){ <br>
echo $e-&gt;getMessage(); <br>
} <br><br>
/**计算商品价格*/ <br>
function this_price($id){ <br>
$connect = connectDB(); <br>
$query = "select price from phome_ecms_news where id = ".$id; <br>
if(!$connect){ <br>
throw new Exception("数据库链接不成功,请检查!"); <br>
} <br>
if(!$result = $connect -&gt; query($query)){ <br>
throw new Exception("查询失败!"); <br>
} <br>
$row = $result -&gt; fetch_assoc(); <br><br>
return $row['price']; <br>
} <br><br>
/**链接数据库*/ <br>
function connectDB(){ <br>
global $phome_db_server,$phome_db_username,$phome_db_password,$phome_db_dbname,$phome_db_char; <br>
$connect = new mysqli($phome_db_server,$phome_db_username,$phome_db_password,$phome_db_dbname); <br>
$connect -&gt; query("set Names ".$phome_db_char); <br>
return $connect;<br>
} <br><br>
/**显示结果Js*/ <br>
function displayResultJs($totalPrice,$totalProducts){ <br>
return "&lt;div class='car'&gt;您的购物车中有 {$totalProducts} 件商品,总计金额 ¥{$totalPrice}元。<br>
点击查看 &lt;a href='/e/ShopSys/buycar/'&gt;结算&amp;gt;&amp;gt;&lt;/a&gt; &lt;/div&gt;"; <br>
} <br>
?&gt;</div>
<p>
 </p>
<p>
希望本文所述对大家的帝国CMS建站有所帮助。</p>
頁: [1]
查看完整版本: 帝国CMS调用购物车商品数量和总价的方法