实用的php购物车程序,实用php购物车程序
实用的php购物车程序,实用php购物车程序
实用的php教程购物车程序
以前有用过一个感觉不错,不过看了这个感觉也很好,所以介绍给需要的朋友参考一下。
//调用实例
require_once 'cart.class.php';
session_start();
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = new Cart;
}
$cart =& $_SESSION['cart'];
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'];
for($i=0;$i
}
}
$items = $cart->getCart();
//打印
echo "
?>
/**
* Cart
*
* 购物车类
*
* @author doodoo
* @package Cart
* @category Cart
* @license PHP License
* @access public
* @version $Revision: 1.10 $
*/
Class Cart{
var $cart;
var $totalCount; //商品总数量
var $totalPrices; //商品总金额
/**
* Cart Constructor
*
* 类的构造函数,使购物车保持稳定的初始化状态
*
* @static
* @access public
* @return void 无返回值
* @param void 无参数
*/
function Cart(){
$this->totalCount = 0;
$this->totalPrice = 0;
$this->cart = array();
}
// }}}
// {{{ add($item)
/**
* 增加商品到当前购物车
*
* @access public
* @param array $item 商品信息(一维数组:array(商品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;
}
reset($item); //这一句是必须的,因为上面的判断已经移动了数组的指标
$key = current($item);
if($key=="") return $this->cart;
if($this->_isExists($key)){ //商品是否已经存在?
$this->cart[$key]['count'] = end($item);
return $this->cart;
}
$this->ca(www.111cn.net)rt[$key]['ID'] = $key;
$this->cart[$key]['name'] = next($item);
$this->cart[$key]['price'] = next($item);
$this->cart[$key]['count'] = next($item);
return $this->cart;
}
// }}}
// {{{ add($item)
/**
* 从当前购物车中取出部分或全部商品
* 当 $key=="" 的时候,清空当前购物车
* 当 $key!=""&&$count=="" 的时候,从当前购物车中拣出商品ID号为 $key 的全部商品
* 当 $key!=""&&$count!="" 的时候,从当前购物车中拣出 $count个 商品ID号为 $key 的商品
*
* @access public
* @param string $key 商品ID
* @return mixed 返回真假或当前购物车内商品的数组
*/
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']cart[$key]);
}
return $this->cart;
}
// }}}
// {{{ modi($key,$value)
/**
* 修改购物车内商品ID为 $key 的商品的数量为 $value
*
* @access public
* @param string $key 商品ID
* @param int $value 商品数量
* @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;
}
// }}}
// {{{ _isExists($key)
/**
* 判断当前购物车中是否存在商品ID号为$key的商品
*
* @access private
* @param string $key 商品ID
* @return bool true or false;
*/
function _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 or false;
*/
function isEmpty(){
return !count($this->cart);
}
// }}}
// {{{ _stat()
/**
* 取得部分统计信息
*
* @access private
* @return bool true or 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;
}
}//End Class Cart
?>
from:http://www.111cn.net/phper/php-gj/39684.htm
MySQL不是什么难点,无非就是些查询、插入之类的语句,关键是在于购物车所选商品的临时增减维护的工作,需要将商品信息序列化后配合前台COOKIE做临时保存,如果想要实现更好的如页面无刷新实时交互操作的效果,还要加上AJAX+JSON技术~~
楼主给的分数~~~只能给你个思路做参考啊~~
这个是php类,只有php里才能调用,而且这个也算不上程序,只是程序过程中的几十分之一

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
