PHP Shopping Cart class cartclassphp definition and usage examples

Source: Internet
Author: User
Tags php definition php programming php regular expression
This article describes the PHP shopping cart class Cart.class.php definition and usage. Share to everyone for your reference, as follows:

Before the developers used JS Technology developed a set of front-end shopping cart (delete add what all use JS), but browser compatibility is not good, and today finally a problem, there is a foreigner shopping products, due to the use of unknown browser, Chrome, opera ... It is possible, therefore, that I have a job and 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 * * Shopping cart class * * @author Doodoo
 
  
 * @package cart * @category cart * @license PHP License * @access public * @version $Revision: 1.10 $ */class cart{ var $cart; var $totalCount; The total quantity of goods var $totalPrices;  Total amount of merchandise/** * Cart Constructor * * class constructor, so that the shopping cart to maintain a stable initialization status * * @static * @access public * @return void no return value * @param void parameterless */function Cart () {$this->totalcount = 0; $this->totalprice = 0; $this->cart = Array ();}//}}}//{ {{Add ($item)/** * Add Item to current CART * * @access public * @param array $item product information (one-dimensional array: Array (Product ID, product name, unit price, number of items) * @return arr AY returns an 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 number"; return $this->cart;} Reset ($item); This sentence is necessary because the above judgment has moved the array of indicators $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 = = "", empty the current cart * when $key! = "" && $count = = "", pick a business from the current shopping cart  Product ID number is $key all items * when $key! = "" && $count! = "", pick out from the current shopping cart $count item ID number $key ITEMS * * @access public * @param string $key Product ID * @return Mixed returns an array of products that are true or false or 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 commodity unset ($this->cart[$key]);} else{//removal of $count commodity $this->cart[$key] [' count ']-= $count, if ($this->cart[$key] [' count ']<=0) unset ($this- >cart[$key]); } return $this->cart; }//}}}//{{{Modi ($key, $value)/** * Modify the number of items in the shopping cart that have the item ID $key $value * * @access public * @param string $key item ID * @pa RAM int $value Number of items * @return Array returns the list of items in the current shopping cart; */Function Modi ($key, $valUE) {if (! $this->_isexists ($key)) return $this->cart ();/////The product is not present, 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 a list of items in the current shopping cart; */function Getcart () {return $this->cart;}//}}}//{{{_isexists ($key)/** * Determine if there is an item with an item ID number $key in the current shopping cart * * @access Private * @param string $key Commodity 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 ()/** * Determines whether the current shopping cart is empty, that is, there is no item * * @access public * @return bool true or false; */function IsEmpty () {return!count ($this->cart);} }}}//{{{_stat ()/** * Get some statistics * * @access private * @return bool True or false; */function _stat () {if ($this->i Sempty ()) return false; foreach ($this->cart as $item) {$thIs->totalcount = @end ($item); $this->totalprices = @prev ($item); } return true; }//}}}//{{{totalprices ()/** * Gets 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 ()/** * Gets the total quantity of all items in the current shopping cart and * * @access public * @return int; */function TotalCount () {if ($this-&gt ; _stat ()) return $this->totalcount; return 0; }}//end Class cart?>
 

Ways to use this class:

<?phpheader ("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 (($_server[' Request_method ']== "POST") && ($_post[' action ']== ' Modi ') {$key = $_post[' key ']; $value = $_post[ ' Value ']; for ($i =0; $i
 
  
   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 [' prices ']; echo " "; $sum = $item [' Count ']* $item [' Price ']; echo " Total: ". Round ($sum, 2); echo " "; } echo ""; echo "
"; echo "
";? >

More about PHP related content readers can view the topic: "Php+mysql Shopping cart Development topic", "PHP Object-oriented Programming tutorial", "PHP Math Skills Summary", "PHP array" operation Skills Daquan, "PHP string ( String) Usage Summary, PHP data structure and algorithm tutorial, PHP programming algorithm Summary, PHP Regular Expression usage summary, and PHP Common database Operations Skills Summary

I hope this article is helpful to you in PHP programming.

The above describes the PHP shopping cart class cartclassphp definition and usage examples, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.