一個簡單的php+Ajax購物車程式碼(1/2)_PHP教程

來源:互聯網
上載者:User
本文章來給大家推薦一個不錯的購物車效果,這裡主要求包括了幾個東西,一個是購物車類用php寫的, 還有一個Ajax操作用到了jquery,還有一個jquery外掛程式thickbox了,下面我們來看看。

購物車類:shop_cart.php
購物車的操作:cart_action.php
首頁:index.html
Ajax操作用到了jquery,還有一個jquery外掛程式thickbox

不多說了你可以先看看效果樣本
shop_cart.php當然是購物車的核心,但是這個類很簡單,因為他又引進了cart_action.php用於對外操作。所以這個類顯得相當精簡。
購物車類shop_cart.php

代碼如下 複製代碼

cart_name = $name;
$this->items = $_SESSION[$this->cart_name];
}

/**
* setItemQuantity() - Set the quantity of an item.
*
* @param string $order_code The order code of the item.
* @param int $quantity The quantity.
*/
function setItemQuantity($order_code, $quantity) {
$this->items[$order_code] = $quantity;
}

/**
* getItemPrice() - Get the price of an item.
*
* @param string $order_code The order code of the item.
* @return int The price.
*/
function getItemPrice($order_code) {
// This is where the code taht retrieves prices
// goes. We'll just say everything costs $9.99 for this tutorial.
return 9.99;
}

/**
* getItemName() - Get the name of an item.
*
* @param string $order_code The order code of the item.
*/
function getItemName($order_code) {
// This is where the code that retrieves product names
// goes. We'll just return something generic for this tutorial.
return 'My Product (' . $order_code . ')';
}

/**
* getItems() - Get all items.
*
* @return array The items.
*/
function getItems() {
return $this->items;
}

/**
* hasItems() - Checks to see if there are items in the cart.
*
* @return bool True if there are items.
*/
function hasItems() {
return (bool) $this->items;
}

/**
* getItemQuantity() - Get the quantity of an item in the cart.
*
* @param string $order_code The order code.
* @return int The quantity.
*/
function getItemQuantity($order_code) {
return (int) $this->items[$order_code];
}

/**
* clean() - Cleanup the cart contents. If any items have a
* quantity less than one, remove them.
*/
function clean() {
foreach ( $this->items as $order_code=>$quantity ) {
if ( $quantity < 1 ) unset($this->items[$order_code]);
}
}

/**
* save() - Saves the cart to a session variable.
*/
function save() {
$this->clean();
$_SESSION[$this->cart_name] = $this->items;
}
}

?>

對於cart_action,他實現了shop_cart類與index的中間作用,用於更新,刪除,增加商品的操作。
cart_action.php

代碼如下 複製代碼

getItemQuantity($_GET['order_code'])+$_GET['quantity'];
$Cart->setItemQuantity($_GET['order_code'], $quantity);
}else{

if ( !empty($_GET['quantity']) ) {
foreach ( $_GET['quantity'] as $order_code=>$quantity){
$Cart->setItemQuantity($order_code, $quantity);
}
}

if ( !empty($_GET['remove']) ) {
foreach ( $_GET['remove'] as $order_code ) {
$Cart->setItemQuantity($order_code, 0);
}
}
}
$Cart->save();

header('Location: cart.php');

?>

還有就是index.html實現對外的操作,也就是添加操作

代碼如下 複製代碼





Shopping Cart









購物車


開啟購物車

添加三個 KWL-JFE 到購物車




還有就是cart.php這是我們的購物車

代碼如下 複製代碼
include('shopping_cart.class.php');
session_start();
$Cart = new Shopping_Cart('shopping_cart');
?>




Shopping Cart







Shopping Cart


hasItems() ) : ?>


您還沒有購物.



載入簡單的購物車





1 2

http://www.bkjia.com/PHPjc/631557.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631557.htmlTechArticle本文章來給大家推薦一個不錯的購物車效果,這裡主要求包括了幾個東西,一個是購物車類用php寫的, 還有一個Ajax操作用到了jquery,還有一...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.