PHP購(gòu)物車種,移植于CodeIgniter
Jun 13, 2016 am 10:39 AM
PHP購(gòu)物車類,移植于CodeIgniter
<?php /** * 購(gòu)物車程序 Modified by CodeIgniter * */class cart { // 對(duì)產(chǎn)品ID和產(chǎn)品名稱進(jìn)行正則驗(yàn)證屬性 var $product_id_rules = '\.a-z0-9_-'; var $product_name_rules = '\.\:\-_ a-z0-9'; // 考慮到漢字,該功能暫不使用 // 私有變量 var $_cart_contents = array(); /** * 構(gòu)造方法 * */ public function __construct() { if ($this->session('cart_contents') !== FALSE) { $this->_cart_contents = $this->session('cart_contents'); } else { // 初始化數(shù)據(jù) $this->_cart_contents['cart_total'] = 0; $this->_cart_contents['total_items'] = 0; } } // -------------------------------- /** * 添加到購(gòu)物車 * * @access public * @param array * @return bool */ function insert($items = array()) { // 檢測(cè)數(shù)據(jù)是否正確 if ( ! is_array($items) OR count($items) == 0) { return FALSE; } // 可以添加一個(gè)商品(一維數(shù)組),也可以添加多個(gè)商品(二維數(shù)組) $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; } } } } // 更新數(shù)據(jù) if ($save_cart == TRUE) { $this->_save_cart(); return TRUE; } return FALSE; } // -------------------------------- /** * 處理插入購(gòu)物車數(shù)據(jù) * * @access private * @param array * @return bool */ function _insert($items = array()) { // 檢查購(gòu)物車 if ( ! is_array($items) OR count($items) == 0) { return FALSE; } // -------------------------------- /* 前四個(gè)數(shù)組索引 (id, qty, price 和name) 是 必需的。 如果缺少其中的任何一個(gè),數(shù)據(jù)將不會(huì)被保存到購(gòu)物車中。 第5個(gè)索引 (options) 是可選的。當(dāng)你的商品包含一些相關(guān)的選項(xiàng)信息時(shí),你就可以使用它。 請(qǐng)使用一個(gè)數(shù)組來保存選項(xiàng)信息。注意:$data['price'] 的值必須大于0 如:$data = array( 'id' => 'sku_123ABC', 'qty' => 1, 'price' => 39.95, 'name' => 'T-Shirt', 'options' => array('Size' => 'L', 'Color' => 'Red') ); */ if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name'])) { return FALSE; } // -------------------------------- // 數(shù)量驗(yàn)證,不是數(shù)字替換為空 $items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty'])); // 數(shù)量驗(yàn)證 $items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty'])); // 數(shù)量必須是數(shù)字或不為0 if ( ! is_numeric($items['qty']) OR $items['qty'] == 0) { return FALSE; } // -------------------------------- // 產(chǎn)品ID驗(yàn)證 if ( ! preg_match("/^[".$this->product_id_rules."]+$/i", $items['id'])) { return FALSE; } // -------------------------------- // 驗(yàn)證產(chǎn)品名稱,考慮到漢字,暫不使用 /* if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name'])) { return FALSE; } */ // -------------------------------- // 價(jià)格驗(yàn)證 $items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price'])); $items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price'])); // 驗(yàn)證價(jià)格是否是數(shù)值 if ( ! is_numeric($items['price'])) { return FALSE; } // -------------------------------- // 屬性驗(yàn)證,如果屬性存在,屬性值+產(chǎn)品ID進(jìn)行加密保存在$rowid中 if (isset($items['options']) AND count($items['options']) > 0) { $rowid = md5($items['id'].implode('', $items['options'])); } else { // 沒有屬性時(shí)直接對(duì)產(chǎn)品ID加密 $rowid = md5($items['id']); } // 檢測(cè)購(gòu)物車中是否有該產(chǎn)品,如果有,在原來的基礎(chǔ)上加上本次新增的商品數(shù)量 $_contents = $this->_cart_contents; $_tmp_contents = array(); foreach ($_contents as $val) { if (is_array($val) AND isset($val['rowid']) AND isset($val['qty']) AND $val['rowid']==$rowid) { $_tmp_contents[$val['rowid']]['qty'] = $val['qty']; } else { $_tmp_contents[$val['rowid']]['qty'] = 0; } } // -------------------------------- // 清除原來的數(shù)據(jù) unset($this->_cart_contents[$rowid]); // 重新賦值 $this->_cart_contents[$rowid]['rowid'] = $rowid; // 添加新項(xiàng)目 foreach ($items as $key => $val) { if ($key=='qty' && isset($_tmp_contents[$rowid][$key])) { $this->_cart_contents[$rowid][$key] = $val+$_tmp_contents[$rowid][$key]; } else { $this->_cart_contents[$rowid][$key] = $val; } } return TRUE; } // -------------------------------- /** * 更新購(gòu)物車 * * @access public * @param array * @param string * @return bool */ function update($items = array()) { // 驗(yàn)證 if ( ! is_array($items) OR count($items) == 0) { 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; } } } } if ($save_cart == TRUE) { $this->_save_cart(); return TRUE; } return FALSE; } // -------------------------------- /** * 處理更新購(gòu)物車 * * @access private * @param array * @return bool */ function _update($items = array()) { if ( ! isset($items['qty']) OR ! isset($items['rowid']) OR ! isset($this->_cart_contents[$items['rowid']])) { return FALSE; } // 檢測(cè)數(shù)量 $items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']); if ( ! is_numeric($items['qty'])) { return FALSE; } if ($this->_cart_contents[$items['rowid']]['qty'] == $items['qty']) { return FALSE; } if ($items['qty'] == 0) { unset($this->_cart_contents[$items['rowid']]); } else { $this->_cart_contents[$items['rowid']]['qty'] = $items['qty']; } return TRUE; } // -------------------------------- /** * 保存購(gòu)物車到Session里 * * @access private * @return bool */ function _save_cart() { unset($this->_cart_contents['total_items']); unset($this->_cart_contents['cart_total']); $total = 0; $items = 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']); $items += $val['qty']; $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']); } $this->_cart_contents['total_items'] = $items; $this->_cart_contents['cart_total'] = $total; if (count($this->_cart_contents) session('cart_contents', array()); return FALSE; } $this->session('cart_contents',$this->_cart_contents); return TRUE; } // -------------------------------- /** * 購(gòu)物車中的總計(jì)金額 * * @access public * @return integer */ function total() { return $this->_cart_contents['cart_total']; } // -------------------------------- /** * 購(gòu)物車中總共的項(xiàng)目數(shù)量 * * * @access public * @return integer */ function total_items() { return $this->_cart_contents['total_items']; } // -------------------------------- /** * 購(gòu)物車中所有信息的數(shù)組 * * 返回一個(gè)包含了購(gòu)物車中所有信息的數(shù)組 * * @access public * @return array */ function contents() { $cart = $this->_cart_contents; unset($cart['total_items']); unset($cart['cart_total']); return $cart; } // -------------------------------- /** * 購(gòu)物車中是否有特定的列包含選項(xiàng)信息 * * 如果購(gòu)物車中特定的列包含選項(xiàng)信息,本函數(shù)會(huì)返回 TRUE(布爾值),本函數(shù)被設(shè)計(jì)為與 contents() 一起在循環(huán)中使用 * * @access public * @return array */ function has_options($rowid = '') { if ( ! isset($this->_cart_contents[$rowid]['options']) OR count($this->_cart_contents[$rowid]['options']) === 0) { return FALSE; } return TRUE; } // -------------------------------- /** * 以數(shù)組的形式返回特定商品的選項(xiàng)信息 * * 本函數(shù)被設(shè)計(jì)為與 contents() 一起在循環(huán)中使用 * * @access public * @return array */ function product_options($rowid = '') { if ( ! isset($this->_cart_contents[$rowid]['options'])) { return array(); } return $this->_cart_contents[$rowid]['options']; } // -------------------------------- /** * 格式化數(shù)值 * * 返回格式化后帶小數(shù)點(diǎn)的值(小數(shù)點(diǎn)后有2位),一般價(jià)格使用 * * @access public * @return integer */ function format_number($n = '') { if ($n == '') { return ''; } $n = trim(preg_replace('/([^0-9\.])/i', '', $n)); return number_format($n, 2, '.', ','); } // -------------------------------- /** * 銷毀購(gòu)物車 * * 這個(gè)函數(shù)一般是在處理完用戶訂單后調(diào)用 * * @access public * @return null */ function destroy() { unset($this->_cart_contents); $this->_cart_contents['cart_total'] = 0; $this->_cart_contents['total_items'] = 0; $this->session('cart_contents', array()); } // -------------------------------- /** * 保存Session * * 須有session_start(); * * @access private * @return bool */ function session($name = 'cart_contents',$value = NULL) { if ($name=='') $name = 'cart_contents'; if ($value == NULL) { return @$_SESSION[$name]; } else { if (!empty($value) && is_array($value)) { $_SESSION[$name] = $value; return TRUE; } else { return FALSE; } } }}?>

ホットAIツール

Undress AI Tool
脫衣畫像を無料で

Undresser.AI Undress
リアルなヌード寫真を作成する AI 搭載アプリ

AI Clothes Remover
寫真から衣服を削除するオンライン AI ツール。

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡(jiǎn)単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中國(guó)語版
中國(guó)語版、とても使いやすい

ゼンドスタジオ 13.0.1
強(qiáng)力な PHP 統(tǒng)合開発環(huán)境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

C 言語における return の使い方は、 1. 戻り値の型が void の関數(shù)については、return 文を使用して関數(shù)の実行を早期に終了することができます; 2. 戻り値の型が void ではない関數(shù)については、 return ステートメントは、関數(shù)の実行を終了するためのものです。結(jié)果は呼び出し元に返されます。 3. 関數(shù)の実行を早期に終了します。関數(shù)內(nèi)で return ステートメントを使用して、関數(shù)の実行を早期に終了することもできます。関數(shù)が値を返さない場(chǎng)合。

ソースコード: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#出力 上記のコードの出力は、単純に次のように結(jié)論付けることができます:finally の前に return が実行されます。バイトコード レベルで何が起こるかを見てみましょう。以下は、case1 メソッドのバイトコードの一部をインターセプトし、ソース コードを比較して、各命令の意味に注釈を付けます。

この記事は、vue ソース コードを解釈するのに役立ち、これを使用して Vue2 のさまざまなオプションのプロパティにアクセスできる理由を紹介します。

JavaScript での return の使用には特定のコード例が必要です。 JavaScript では、return ステートメントを使用して関數(shù)から返される値を指定します。関數(shù)の実行を終了するために使用できるだけでなく、関數(shù)が呼び出された場(chǎng)所に値を返すこともできます。 return ステートメントには次の一般的な用途があります。 値を返す return ステートメントは、関數(shù)が呼び出された場(chǎng)所に値を返すために使用できます。簡(jiǎn)単な例を次に示します。 functionadd(a,b){

Vue3.2 セットアップ構(gòu)文シュガーは、単一ファイル コンポーネント (SFC) で結(jié)合された API を使用して、Vue3.0 の面倒なセットアップを解決するコンパイル時(shí)構(gòu)文シュガーです。宣言された変數(shù)、関數(shù)、インポートによって導(dǎo)入されたコンテンツは、インポートによって公開されます。使用上の問題點(diǎn) 1. 宣言した変數(shù)、関數(shù)、import で導(dǎo)入した?jī)?nèi)容を使用中に return する必要はなく、糖衣構(gòu)文を使用することができます。 // 導(dǎo)入した?jī)?nèi)容をインポート import{getToday }from'./utils'//variable constmsg='Hello !'//function func

JavaScript 関數(shù)は、外部と対話するための 2 つのインターフェースを提供し、パラメータは外部情報(bào)を受け取る入り口として機(jī)能し、戻り値は演算結(jié)果を外部にフィードバックする出口として機(jī)能します。次の記事では、JavaScript 関數(shù)の戻り値を理解し、return ステートメントの使用法を簡(jiǎn)単に分析します。

同僚は、これによって指摘されたバグのために立ち往生しました。Vue2 のこの指摘の問題により、アロー関數(shù)が使用され、その結(jié)果、対応する props を取得できなくなりました。私がそれを彼に紹介したとき、彼はそれを知りませんでした。その後、私はわざとフロントエンド コミュニケーション グループに目を向けました。これまでのところ、フロントエンド プログラマーの少なくとも 70% はまだそれを理解していません。今日私はそれを共有しますyou this link. もしすべてが間違っている場(chǎng)合 まだ學(xué)習(xí)していない場(chǎng)合は、大きな口を與えてください。

Python の戻り値 return の使い方は、関數(shù)が return ステートメントを?qū)g行すると、実行が直ちに停止され、関數(shù)が呼び出された場(chǎng)所に指定された値が返されます。詳細(xì)な使用法: 1. 単一の値を返す; 2. 複數(shù)の値を返す; 3. null 値を返す; 4. 関數(shù)の実行を早期に終了する。
