Home >Backend Development >PHP Tutorial >Practical php shopping cart program, practical php shopping cart program_PHP tutorial
Practical php tutorial shopping cart program
I have used one before and it felt good, but after reading this I feel... Very good, so I will introduce it to friends who need it for reference.
//Calling instance
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();
//Print
echo "
?>
/**
* Cart
*
* 购物车类
*
* @author doodoo
* @package Cart
* @category Cart
* @license PHP License
* @access public
* @version $Revision: 1.10 $
*/
Class Cart{
var $cart;
var $totalCount; //Total quantity of goods
var $totalPrices; //Total amount of goods
/**
* Cart Constructor
*
* Constructor of the class to keep the shopping cart in a stable initialization state
*
* @static
* @access public
* @return void no return value
* @param void no parameter
*/
function Cart(){
$this->totalCount = 0;
$this->totalPrice = 0;
$this-> ;cart = array();
}
// }}}
// {{{ add($item)
/**
* Add products to the current shopping cart
*
* @access public
* @param array $item product information (one-dimensional array: array(product ID, product name, product unit price, product quantity ))
* @return array Returns the array of items in the current shopping cart
*/
function add($item){
if(!is_array($item)||is_null($item)) return $this->cart;
if(!is_numeric(end($item))||(!is_numeric(prev($item)))) {
echo "Price and quantity must be numbers";
return $this->cart ;
}
reset($item); //This sentence is necessary because the above judgment has moved the array index
$key = current($item);
if($ key=="") return $this->cart;
if($this->_isExists($key)){ //Does the product already exist?
$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)
/**
* Remove some or all items from the current shopping cart
* When $key=="", clear the current shopping cart
* When $key!=""&&$count=="" When , pick out all the products with the product ID number $key from the current shopping cart
* When $key!=""&&$count!="" , pick out $count items from the current shopping cart Product with product ID number $key
*
* @access public
* @param string $key product ID
* @return mixed Returns true or false or an array of products in the current shopping cart
*/
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)
/**
* Modify the quantity of the product with product ID $key in the shopping cart to $value
*
* @access public
* @param string $key product ID
* @param int $ value item quantity
* @return array Returns the array of items in the current shopping cart;
*/
function modi($key,$value){
if(!$this->_isExists($key)) return $this->cart(); //不存在此商品,直接返回
if($value<=0){ // value 太小,全部删除
unset($this->cart[$key]);
return $this->cart;
}
$this->cart[$key]['count'] = $value;
return $this->cart;
}
/**
* Returns an array of items in the current shopping cart
*
* @access public
* @return array Returns an array of items in the current shopping cart;
*/
function getCart(){
return $this->cart;
}
// }}}
// {{{ _isExists($key)
/**
* Determine whether there is a product with product ID $key in the current shopping cart
*
* @access private
* @param string $key product 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()
/**
* Determine whether the current shopping cart is empty, that is, there are no products
*
* @access public
* @return bool true or false;
*/
function isEmpty(){
return !count($this->cart);
}
// }}}
// {{{ _stat()
/**
* Get some statistical information
*
* @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()
/**
* Get the total amount of all items in the current shopping cart
*
* @access public
* @return float return amount;
*/
function totalPrices(){
if($this->_stat())
return $this->totalPrices;
return 0;
}
// }}}
// {{{ isEmpty()
/**
* Get the total quantity of all items in the current shopping cart and
*
* @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技术~~
楼主给的分数~~~只能给你个思路做参考啊~~
This is a php class, which can only be called in php, and this is not a program, it is just a few tenths of the program process