PHP Shopping Cart class Cart.class.php definitions and usage examples _php tips

Source: Internet
Author: User
Tags php definition php programming php regular expression prev

This article describes the Cart.class.php definition and usage of the PHP shopping cart class. Share to everyone for your reference, specific as follows:

The previous developers used JS technology to develop a set of front-end shopping cart (delete add what all use JS), but the browser is not compatible, today finally problems, there is a foreigner shopping products, due to the use of the unknown browser, Chrome, opera ... It is possible, therefore, that I have a job to rewrite the shopping cart.

Do not intend to use JS, directly consider PHP.

Found a shopping cart class, easy to use.

Cart.class.php Source:

<?php/** * Cart * * @author doodoo<pwtitle @yahoo. com.cn= "" > * @package cart * @category Cart
 * @license PHP License * @access public * @version $Revision: 1.10 $ */Class cart{var $cart; var $totalCount; Total quantity of commodities var $totalPrices;
  Total amount of merchandise/** * Cart Constructor * * Class constructor that keeps 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 merchandise to current cart * * @access public * @param array $item commodity information (one-dimensional array: Array (commodity ID, commodity name, commodity price, Number of items) * @return Array returns an array of items in the current Cart/function Add ($item) {if (!is_array ($item) | |
 Is_null ($item)) return $this->cart; if (!is_numeric ($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 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)/** * Take some or all of the items out of the current cart * when $key = = "", empty the current cart * when $key!= "" && $count = "", from the current purchase The goods in the car to pick out the Product ID number $key All goods * when $key!= "" "&& $count!=" ", from the current shopping cart, pick out $count commodity ID number for $key goods * * @access public * @pa RAM string $key Commodity ID * @return Mixed returns a true or False or an array of 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 category of commodity unset ($this->cart[$key]);
 }else{//Remove $count commodity $this->cart[$key [' count ']-= $count;
 if ($this->cart[$key] [' count ']<=0] unset ($this->cart[$key]);
 return $this->cart;
}//}}//{{Modi ($key, $value) /** * The number of items in the shopping cart for which the product ID is $key is $value * * @access public * @param string $key commodity ID * @param int $value Commodity quantity * @retu
 The RN array returns a list of the items in the current shopping cart;
 {//value too small to 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 cart * * @access public * @return Array returns an array of items in the current shopping cart;
 */function Getcart () {return $this->cart; }//{{_isexists ($key)/** * Determine if there is a commodity ID number $key in the current cart * * @access private * @param string $key commodity ID * @r
 Eturn 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 ()/** * Determines whether the current shopping cart is empty, that is, there is no merchandise * @access public * @return bool true or false;
 */Function IsEmpty () {Return!count ($this->cart);
 }//}}//{{{_stat ()/** * Obtains partial statistics * * @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 ()/** * Obtains the total amount of all merchandise in the current cart * * @access public * @return float return amount;
 */function Totalprices () {if ($this->_stat ()) return $this->totalprices;
 return 0;
 }//}}//{{{IsEmpty ()/** * Obtains the total quantity of all merchandise for the current cart and * * @access public * @return int;
 */function TotalCount () {if ($this->_stat ()) return $this->totalcount;
 return 0;

 }}//end Class Cart?>

Ways to use this class:

<?php header ("Content-type:text/html;charset=utf8");
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 ($_serv
 er[' 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 "";
SetLocale (lc_monetary, ' it_it ');
 foreach ($items as $item) {echo ""; echo "<table border=" 1 "><tbody><tr><form action=" \ "Index.php\" "method=" \ "Post\?? = "" ></form><td>id: ". $item [' ID ']." <input type= "hidden" value=. $item [' ID ']. "Name=" key[] ">"; EcHo "</td><td> Products:". $item [' name ']; echo "</td><td> Price:". $item [' prices ']; echo "</td><td><input value=". $item [' Count ']. "Name=" value[] ">"; $sum = $item [' Count ']* $item [' Price ']; echo "</td><td> total:". Round ($sum, 2); echo "</td><td><input onclick=" \ location= ' action=remove&key= ". $item [' ID ']." ' \ "type=" button "value=" delete ">"; echo "<input type=" hidden "value=" Modi "name=" action ">"; echo "</td></tr><tr><td colspan=" 7 "><input type=" Submit "value=" submitted query Content ">"; echo "</td></tr></tbody></table>";?>  

For more information about PHP interested readers can view the site topics: "Php+mysql Shopping cart Development topic", "PHP object-oriented Program Design Primer", "PHP Mathematical Calculation Skills Summary", "PHP Array" Operation Techniques Encyclopedia, "PHP string ( String) Usage summary, "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", "PHP Regular Expression Usage Summary", and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.