Home > Backend Development > PHP Tutorial > Practical php shopping cart program_PHP tutorial

Practical php shopping cart program_PHP tutorial

WBOY
Release: 2016-07-20 11:10:00
Original
1087 people have browsed it

Practical PHP Tutorial Shopping Cart Program
I have used one before and it felt good, but I also feel good after reading this one, 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->modi($key[$i],$value[$i]);
}
}

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

";
setlocale(LC_MONETARY, 'it_IT') ;
foreach($items as $item){
echo "";
echo "
ID: ".$item['ID']."";
echo "
Product: ".$item['name'];
echo "
Unit price: ".$item['price'];
echo "
";
$sum = $item['count']*$item['price'];
echo " echo "
";
}
echo "";
echo "
";
echo "
";


?> ;




ID:Product name:
Unit price:
Quantity:


/**
 * 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-> ;cart[$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==""){ //Remove this category of goods
unset( $this->cart[$key]);
}else{ //Remove $count items
$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(); //This product does not exist, return directly
if($value<=0){ // value is too small, delete all
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 the product ID number $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()

    /**
* Obtain 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
?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444755.htmlTechArticle实用的php教程购物车程序 以前有用过一个感觉不错,不过看了这个感觉也很好,所以介绍给需要的朋友参考一下。 ?php //调用实例 require_...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template