本篇主要介紹php仿寫購物車及分析其功能,有興趣的朋友參考下,希望對大家有幫助。
這裡仿寫CodeIgniter的購物車類別
購物車基本功能:
1) 將物品加入購物車
2) 從購物車中刪除物品
3) 更新購物車物品資訊【 1/-1】
4) 對購物車物品進行統計
1. 總項目
2. 總數量
3 . 總金額
5) 對購物單項物品的數量及金額進行統計
6) 清空購物車
##cart.php檔案如下:##<?php
/**
*
* @author quanshuidingdang
*/
class Cart {
//物品id及名称规则,调试信息控制
private $product_id_rule = '\.a-z0-9-_'; //小写字母 | 数字 | ._-
private $product_name_rule = '\.\:a-z0-9-_';//小写字母 | 数字 | ._-:
private $debug = TRUE;
//购物车
private $_cart_contents = array();
/**
* 构造函数
*
* @param array
*/
public function __construct() {
//是否第一次使用?
if(isset($_SESSION['cart_contents'])) {
$this->_cart_contents = $_SESSION['cart_contents'];
} else {
$this->_cart_contents['cart_total'] = 0;
$this->_cart_contents['total_items'] = 0;
}
if($this->debug === TRUE) {
//$this->_log("cart_create_success");
}
}
/**
* 将物品加入购物车
*
* @access public
* @param array 一维或多维数组,必须包含键值名:
id -> 物品ID标识,
qty -> 数量(quantity),
price -> 单价(price),
name -> 物品姓名
* @return bool
*/
public function insert($items = array()) {
//输入物品参数异常
if( ! is_array($items) OR count($items) == 0) {
if($this->debug === TRUE) {
$this->_log("cart_no_items_insert");
}
return FALSE;
}
//物品参数处理
$save_cart = FALSE;
if(isset($items['id'])) {
if($this->_insert($items) === TRUE) {
$save_cart = TRUE;
}
} else {
foreach($items as $val) {
if(is_array($val) AND isset($val['id'])) {
if($this->_insert($val) == TRUE) {
$save_cart = TRUE;
}
}
}
}
//当插入成功后保存数据到session
if($save_cart) {
$this->_save_cart();
return TRUE;
}
return FALSE;
}
/**
* 更新购物车物品信息
*
* @access public
* @param array
* @return bool
*/
public function update($items = array()) {
//输入物品参数异常
if( !is_array($items) OR count($items) == 0) {
if($this->debug === TRUE) {
$this->_log("cart_no_items_insert");
}
return FALSE;
}
//物品参数处理
$save_cart = FALSE;
if(isset($items['rowid']) AND isset($items['qty'])) {
if($this->_update($items) === TRUE) {
$save_cart = TRUE;
}
} else {
foreach($items as $val) {
if(is_array($val) AND isset($val['rowid']) AND isset($val['qty'])) {
if($this->_update($val) === TRUE) {
$save_cart = TRUE;
}
}
}
}
//当更新成功后保存数据到session
if($save_cart) {
$this->_save_cart();
return TRUE;
}
return FALSE;
}
/**
* 获取购物车物品总金额
*
* @return int
*/
public function total() {
return $this->_cart_contents['cart_total'];
}
/**
* 获取购物车物品种类
*
* @return int
*/
public function total_items() {
return $this->_cart_contents['total_items'];
}
/**
* 获取购物车
*
* @return array
*/
public function contents() {
return $this->_cart_contents;
}
/**
* 获取购物车物品options
*
* @param string
* @return array
*/
public function options($rowid = '') {
if($this->has_options($rowid)) {
return $this->_cart_contents[$rowid]['options'];
} else {
return array();
}
}
/**
* 清空购物车
*
*/
public function destroy() {
unset($this->_cart_contents);
$this->_cart_contents['cart_total'] = 0;
$this->_cart_contents['total_items'] = 0;
unset($_SESSION['cart_contents']);
}
/**
* 判断购物车物品是否有options选项
*
* @param string
* @return bool
*/
private function has_options($rowid = '') {
if( ! isset($this->_cart_contents[$rowid]['options']) OR count($this->_cart_contents[$rowid]['options']) === 0) {
return FALSE;
}
return TRUE;
}
/**
* 插入数据
*
* @access private
* @param array
* @return bool
*/
private function _insert($items = array()) {
//输入物品参数异常
if( ! is_array($items) OR count($items) == 0) {
if($this->debug === TRUE) {
$this->_log("cart_no_data_insert");
}
return FALSE;
}
//如果物品参数无效(无id/qty/price/name)
if( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name'])) {
if($this->debug === TRUE) {
$this->_log("cart_items_data_invalid");
}
return FALSE;
}
//去除物品数量左零及非数字字符
$items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));
$items['qty'] = trim(preg_replace('/^([0]+)/i', '', $items['qty']));
//如果物品数量为0,或非数字,则我们对购物车不做任何处理!
if( ! is_numeric($items['qty']) OR $items['qty'] == 0) {
if($this->debug === TRUE) {
$this->_log("cart_items_data(qty)_invalid");
}
return FALSE;
}
//物品ID正则判断
if( ! preg_match('/^['.$this->product_id_rule.']+$/i', $items['id'])) {
if($this->debug === TRUE) {
$this->_log("cart_items_data(id)_invalid");
}
return FALSE;
}
//物品名称正则判断
if( ! preg_match('/^['.$this->product_name_rule.']+$/i', $items['name'])) {
if($this->debug === TRUE) {
$this->_log("cart_items_data(name)_invalid");
}
return FALSE;
}
//去除物品单价左零及非数字(带小数点)字符
$items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));
$items['price'] = trim(preg_replace('/^([0]+)/i', '', $items['price']));
//如果物品单价非数字
if( ! is_numeric($items['price'])) {
if($this->debug === TRUE) {
$this->_log("cart_items_data(price)_invalid");
}
return FALSE;
}
//生成物品的唯一id
if(isset($items['options']) AND count($items['options']) >0) {
$rowid = md5($items['id'].implode('', $items['options']));
} else {
$rowid = md5($items['id']);
}
//加入物品到购物车
unset($this->_cart_contents[$rowid]);
$this->_cart_contents[$rowid]['rowid'] = $rowid;
foreach($items as $key => $val) {
$this->_cart_contents[$rowid][$key] = $val;
}
return TRUE;
}
/**
* 更新购物车物品信息(私有)
*
* @access private
* @param array
* @return bool
*/
private function _update($items = array()) {
//输入物品参数异常
if( ! isset($items['rowid']) OR ! isset($items['qty']) OR ! isset($this->_cart_contents[$items['rowid']])) {
if($this->debug == TRUE) {
$this->_log("cart_items_data_invalid");
}
return FALSE;
}
//去除物品数量左零及非数字字符
$items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']);
$items['qty'] = preg_replace('/^([0]+)/i', '', $items['qty']);
//如果物品数量非数字,对购物车不做任何处理!
if( ! is_numeric($items['qty'])) {
if($this->debug === TRUE) {
$this->_log("cart_items_data(qty)_invalid");
}
return FALSE;
}
//如果购物车物品数量与需要更新的物品数量一致,则不需要更新
if($this->_cart_contents[$items['rowid']]['qty'] == $items['qty']) {
if($this->debug === TRUE) {
$this->_log("cart_items_data(qty)_equal");
}
return FALSE;
}
//如果需要更新的物品数量等于0,表示不需要这件物品,从购物车种清除
//否则修改购物车物品数量等于输入的物品数量
if($items['qty'] == 0) {
unset($this->_cart_contents[$items['rowid']]);
} else {
$this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];
}
return TRUE;
}
/**
* 保存购物车数据到session
*
* @access private
* @return bool
*/
private function _save_cart() {
//首先清除购物车总物品种类及总金额
unset($this->_cart_contents['total_items']);
unset($this->_cart_contents['cart_total']);
//然后遍历数组统计物品种类及总金额
$total = 0;
foreach($this->_cart_contents as $key => $val) {
if( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty'])) {
continue;
}
$total += ($val['price'] * $val['qty']);
//每种物品的总金额
$this->_cart_contents[$key]['subtotal'] = ($val['price'] * $val['qty']);
}
//设置购物车总物品种类及总金额
$this->_cart_contents['total_items'] = count($this->_cart_contents);
$this->_cart_contents['cart_total'] = $total;
//如果购物车的元素个数少于等于2,说明购物车为空
if(count($this->_cart_contents) <= 2) {
unset($_SESSION['cart_contents']);
return FALSE;
}
//保存购物车数据到session
$_SESSION['cart_contents'] = $this->_cart_contents;
return TRUE;
}
/**
* 日志记录
*
* @access private
* @param string
* @return bool
*/
private function _log($msg) {
return @file_put_contents('cart_err.log', $msg, FILE_APPEND);
}
}
/*End of file cart.php*/
/*Location /htdocs/cart.php*/
<?php
session_start();
require_once('cart.php');
$items = array(
0 => array(
'id' => 'sp001',
'qty' => 20,
'price' => '10.50',
'name' => 'a002',
'options' => array(
'made' => 'china',
'company' => 'bgi'
)
),
1 => array(
'id' => 'sp002',
'qty' => 1,
'price' => '3.50',
'name' => 'b002'
)
);
$arr = array(
'rowid' => '86dbb7cb58a667558b4bbb1f60330028',
'qty' => 21
);
$cart = new Cart();
$cart->insert($items);
//var_dump($cart->contents());
$cart->update($arr);
var_dump($cart->contents());
//$cart->destroy();
//var_dump($_SESSION['cart_contents']);
/*end of php*/
:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。
相關推薦:php針對檔案的遞歸操作的方法php結合session操作資料庫的方法php如何將遠端圖片本地化
以上是php仿寫購物車及分析其功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

tomodifyDataNaphPsession,startTheSessionWithSession_start(),然後使用$ _sessionToset,修改,orremovevariables.1)startThesession.2)setthesession.2)使用$ _session.3)setormodifysessessvariables.3)emovervariableswithunset()

在PHP會話中可以存儲數組。 1.啟動會話,使用session_start()。 2.創建數組並存儲在$_SESSION中。 3.通過$_SESSION檢索數組。 4.優化會話數據以提升性能。

PHP會話垃圾回收通過概率機制觸發,清理過期會話數據。 1)配置文件中設置觸發概率和會話生命週期;2)可使用cron任務優化高負載應用;3)需平衡垃圾回收頻率與性能,避免數據丟失。

PHP中追踪用戶會話活動通過會話管理實現。 1)使用session_start()啟動會話。 2)通過$_SESSION數組存儲和訪問數據。 3)調用session_destroy()結束會話。會話追踪用於用戶行為分析、安全監控和性能優化。

利用數據庫存儲PHP會話數據可以提高性能和可擴展性。 1)配置MySQL存儲會話數據:在php.ini或PHP代碼中設置會話處理器。 2)實現自定義會話處理器:定義open、close、read、write等函數與數據庫交互。 3)優化和最佳實踐:使用索引、緩存、數據壓縮和分佈式存儲來提升性能。

phpsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIdStoredInAcookie.here'showtomanageThemeffectionaly:1)startAsessionWithSessionWwithSession_start()和stordoredAtain $ _session.2)

在PHP中,遍歷會話數據可以通過以下步驟實現:1.使用session_start()啟動會話。 2.通過foreach循環遍歷$_SESSION數組中的所有鍵值對。 3.處理複雜數據結構時,使用is_array()或is_object()函數,並用print_r()輸出詳細信息。 4.優化遍歷時,可採用分頁處理,避免一次性處理大量數據。這將幫助你在實際項目中更有效地管理和使用PHP會話數據。

會話通過服務器端的狀態管理機制實現用戶認證。 1)會話創建並生成唯一ID,2)ID通過cookies傳遞,3)服務器存儲並通過ID訪問會話數據,4)實現用戶認證和狀態管理,提升應用安全性和用戶體驗。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版
好用的JavaScript開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具