ホームページ  >  記事  >  バックエンド開発  >  実践的なPHPショッピングカートプログラム_PHPチュートリアル

実践的なPHPショッピングカートプログラム_PHPチュートリアル

WBOY
WBOYオリジナル
2016-07-20 11:10:00992ブラウズ

実践的なPHPチュートリアルショッピングカートプログラム
以前にも使ったことがあり、とても良いと感じましたが、こちらも読んでとても良い気分になったので、参考に必要な友人に紹介します。

//インスタンスを呼び出す
require_once 'cart.class.php';
session_start();
if(!isset($_SESSION['cart'])) {
$_SESSION['cart' ] = 新しいカート;
}
$cart =& $_SESSION['カート'];

if( ($_SERVER['REQUEST_METHOD']=="POST")&&($_POST['action']==' add') ){
$p = $_POST['p'];
$items = $cart->add($p);
}
if( ($_GET['action']=='remove' )&&($_GET['key']!="") ) {
$items = $cart->remove($_GET['key']);
}

if( ($_SERVER['REQUEST_METHOD' ]=="POST")&&($_POST['action']=='modi') ){
$key = $_POST['key'];
$value = $_POST['value'];
($i=0;$i $items = $cart->modi($key[$i],$value[$i]);
}
}

$items = $cart->getCart();
//Print
echo "

";
setlocale(LC_MONETARY, 'it_IT');
foreach($items as $item){
echo "";
echo "";
echo "
ID:".$item['ID']."";
echo "
製品:".$item['name'];
echo "
ユニット価格 :".$item['price'];
echo "
";
$sum = $item['count']*$item['price'];
echo "
Total:".round($sum,2);
echo "
";
}
echo " " ;
echo "
" ;


?>




ID:商品名:
単価:
数量:< ;input type ="text" name="p[]" />


/**
* カート
*
* ショッピング カート カテゴリ
*
* @author doodoo
* @package Cart
* @category Cart
* @license PHP License
* @access public
* @バージョン $リビジョン: 1.10 $
*/
Class Cart{

var $cart;
var $totalCount; //商品の合計数量
var $totalPrices; // **

* カートコンストラクター

*
* ショッピングカートを安定した初期化状態に保つためのクラスのコンストラクター
パラメータなし
*/
function Cart(){
$this->totalCount = 0;
$this->totalPrice = 0;
$this->cart = array();
}

/ / }}}
// {{{ add($item)

/**
* 現在のショッピングカートに商品を追加します
*
* @access public
* @param array $item 商品情報(一次元配列:配列(商品ID、商品名、商品単価、商品数量))
* @ return array 現在のショッピングカート内のアイテムの配列を返します
*/
function add($item){
if(!is_array($item)||is_null($item)) return $this->cart;
if(!is_numeric(end( $item))||(!is_numeric(prev($item)))) {
echo "価格と数量は数値である必要があります";
return $this->cart;
}
restart($item); //この文が必要なのは、上記の判断により配列のインデックスが移動したためです
$key = current($item);
if($key=="") return $this->cart;
if($this- > ;_isExists($key)){ //製品はすでに存在しますか?
$this->cart[$key]['count'] = end($item);
return $this->cart;
}

$this->cart[$key]['ID' ] = $key;
$this->カート[$key]['名前'] = next($item);
$this->カート[$key]['価格'] = next($item) ;
$this->cart[$key]['count'] = next($item);

return $this->cart;
}

// }}}
// {{{ 追加($item)

/**
* 現在のショッピング カートから一部またはすべての商品を削除します
* $key=="" の場合、現在のショッピング カートをクリアします
* $key!=""&&$count=="" の場合、現在のショッピング カートから商品を削除しますショッピングカート 商品ID番号$keyの商品を全てカートから取り出します
※$key!=""&&$count!=""の場合、現在の買い物から商品ID番号$keyの商品を$count個取り出しますカート
*
* @access public
* @param string $key product ID
* @returnmixed true または false、または現在のショッピング カート内のアイテムの配列を返します
*/
function Remove($key="",$count=""){
if($key=="") {
$this->cart = array();
return true;
}
if(!array_key_exists($key,$this->cart)) return false;
if($count==""){ //このカテゴリの商品を削除します
unset ($this->cart[$key]);
}else{ //$count 個のアイテムを削除します
$this->cart[$key]['count'] -= $count;
if( $this- >cart[$key]['count']<=0) unset($this->cart[$key]);
}
return $this->cart;
}

/ / }} }
// {{{ modi($key,$value)

/**
* ショッピングカート内の商品ID $key の商品の数量を $value に変更します
*
* @access public
* @param string $key product ID
* @param int $value productquantum
* @return arrayショッピング カート内の現在のアイテムの配列を返します;
*/
function modi($key,$value){
if(!$this->_isExists( $key) ) return $this->cart(); //この商品は存在しません、直接返します
if($value unset($this->cart) [$key]);
return $this->cart;
}
$this->cart[$key]['count'] = $value;
return $this->cart;
}


/**
* 現在のショッピング カート内のアイテムの配列を返します
*
* @access public
* @return array 現在のショッピング カート内のアイテムの配列を返します;
*/
function getCart(){
return $this->cart;
}

}}}
// /**

* 現在のショッピングカートに製品 ID 番号 $key の製品があるかどうかを確認します

*
* @access private
* @param string $key product ID
* @return bool true または false;
*/
関数 _isExists($key)
{
if(isset($this->cart[$key])&&!empty($this->cart[$key])&&array_key_exists($key,$ this->cart))
return true;
return false;
}

// }}}

// {{{ isEmpty()

/**
* 現在のショッピング カートが空かどうか、つまり商品がないかどうかを判断します
*
* @access public
* @return bool true または false;
*/
function isEmpty(){
return !count($this->cart);
}

// }}}
// {{{ _stat()

/* *
* 統計情報を取得します
*
* @access private
* @return bool true または false;
*/
function _stat(){
if($this->isEmpty()) return false;
foreach($this->cart as $item){
$this->totalCount = @ end($item);
$this->totalPrices = @prev($item);
}
return true;
}

// }}}
// {{{ totalPrices()

/**
* 現在のショッピングカート内の全商品の合計金額を取得します
*
* @access public
* @return float 返品金額;
*/
function totalPrices(){
if($this->_stat())
return $this->totalPrices;
return 0;
}

// }}}
// {{ { isEmpty()

/**
* 現在のショッピングカート内の全商品の合計数量を取得します
*
* @access public
* @return int ;
*/
function totalCount(){
if($this->_stat())
return $this->totalCount; 
return 0;
}


}//クラスカートを終了
?>


www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/444755.html技術記事実用的な php 教程の购物车プログラム以前は役に立ちましたが、この感覚も素晴らしいものでした。
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。