A Practical PHP Shopping cart Program

Source: Internet
Author: User
Tags prev

Practical PHP Tutorial Shopping Cart Program
Used to have a good feeling, but see this feeling is also very good, so introduce to the needs of friends reference.

<?php
Invoke 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 <count ($key), $i) {
$items = $cart->modi ($key [$i], $value [$i]);
}
}

$items = $cart->getcart ();
//Print
echo "<table border=1>";
SetLocale (lc_monetary, ' it_it ');
foreach ($items as $item) {
echo "<tr><form method=" post "action=" tmp.php ">";
echo "<td> ID: ". $item [' id ']." <input Type=hidden name=key[] value= ". $item [' ID ']." > ";
Echo <td> Products: ". $item [' name '];
Echo <td> Unit Price: ". $item [' prices '];
echo "<td><input type=text name=value[] value=". $item [' Count ']. " > ";
$sum = $item [' Count ']* $item [' Price '];
Echo <td> total: ". Round ($sum, 2);
echo "<td><input type=button value= ' delete ' onclick= ' location= '? action=remove&key= '. $item [' ID ']." ' " > ";
}
Echo "<input type=hidden name=action value=modi>";
echo "<tr><td colspan=7><input type=submit/>";
echo "</td></form></tr></table>";


?>
<form method= "POST" action= "tmp.php" >
Id:<input type= "text" Name= "p[]"/>
Name: <input type= "text" Name= "p[]"/>
Price: <input type= "text" Name= "p[]"/>
Quantity: <input type= "text" Name= "p[]"/>
<input Type=hidden name=action value=add>
<input type= "Submit"/>
</form>

<?
/**
* Cart
*
* Shopping Cart Category
*
* @author Doodoo<[email protected]>
* @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
*
* class constructor, so that the shopping cart to maintain 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 item to current shopping cart
*
* @access Public
* @param array $item product information (one-dimensional array: Array (Product ID, product name, commodity unit price, number of items))
* @return Array returns the list 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 number";
return $this->cart;
}
Reset ($item); This sentence is necessary because the above judgment has moved the indicator of the array
$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 of the items from your current shopping cart
* when $key = = "", Empty the current cart
* When $key! = "&& $count = =" ", Pick all items from your current cart with the item ID number $key
* When $key! = "&& $count! =" ", pick a $count item ID number $key from your current cart
*
* @access Public
* @param string $key Product ID
* @return Mixed returns an array of TRUE or false items 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 type of product
unset ($this->cart[$key]);
}else{//Removal of $count items
$this->cart[$key [' count ']-= $count;
if ($this->cart[$key] [' count ']<=0) unset ($this->cart[$key]);
}
return $this->cart;
}

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

/**
* Change the number of items in the cart with the product ID $key $value
*
* @access Public
* @param string $key Product ID
* @param int $value number of items
* @return Array Returns a list of items in the current shopping cart;
*/
Function Modi ($key, $value) {
if (! $this->_isexists ($key)) return $this->cart (); This product does not exist and is returned 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 a list of items in the current shopping cart;
*/
function Getcart () {
return $this->cart;
}

// }}}
{{{_isexists ($key)

/**
* Determine if there is a 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 if the current shopping cart is empty, i.e. there is no product
*
* @access Public
* @return bool True or false;
*/
function IsEmpty () {
Return!count ($this->cart);
}

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

/**
* Access to 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 ()}

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

A Practical PHP Shopping cart Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.