Php uses cookies to implement shopping cart. cookies shopping cart_php tutorial

Source: Internet
Author: User
Php uses cookies to implement shopping cart. Php uses cookies to implement shopping cart. This article describes how php uses cookies to implement shopping cart. Share it with you for your reference. The specific analysis is as follows: p php uses cookies to implement the shopping cart method.

This example describes how php uses cookies to implement shopping cart. Share it with you for your reference. The specific analysis is as follows:

Php shopping cart is used on e-commerce websites. it is similar to a shopping cart in a supermarket. after selecting a commodity, put it in your own shopping cart and wait for it to go to the counter for settlement, this php shopping cart is based on this principle. if you are interested, you can check that the instance uses cookies. the code is as follows:

The code is as follows:

<? Php
/**
* The storage cycle of shopping cart cookies is one day. note: The browser must support cookies for use.
*/
Class cartapi {
Private $ cartarray = array (); // Two-dimensional array for storing the shopping cart
Private $ cartcount; // count the number of shopping carts
Public $ expires = 86400; // cookie expiration time. if it is 0, it is not saved to the local device, in seconds.
/**
* If $ id is not empty, the constructor is directly added to the shopping cart.
*
*/
Public function _ construct ($ id = "", $ name = "", $ price1 = "", $ price2 = "", $ price3 = "", $ count = "", $ image = "", $ expires = 86400 ){
If ($ id! = "" & Is_numeric ($ id )){
$ This-> expires = $ expires;
$ This-> addcart ($ id, $ name, $ price1, $ price2, $ price3, $ count, $ image );
}
}
/**
* Add items to the shopping cart
*
* @ Param int $ id of the product
* @ Param string $ name Product name
* @ Param decimal $ price1 product price
* @ Param decimal $ price2 product price
* @ Param decimal $ price3 product price
* @ Param int $ count number of items
* @ Param string $ image product image
* @ Return if the product exists, add 1 to the original quantity and return false.
*/
Public function addcart ($ id, $ name, $ price1, $ price2, $ price3, $ count, $ image ){
$ This-> cartarray = $ this-> cartview (); // reads data and writes it to an array.
If ($ this-> checkitem ($ id) {// check whether the product exists
$ This-> modifycart ($ id, $ count, 0); // add $ count to the number of items
Return false;
}
$ This-> cartarray [0] [$ id] = $ id;
$ This-> cartarray [1] [$ id] = $ name;
$ This-> cartarray [2] [$ id] = $ price1;
$ This-> cartarray [3] [$ id] = $ price2;
$ This-> cartarray [4] [$ id] = $ price3;
$ This-> cartarray [5] [$ id] = $ count;
$ This-> cartarray [6] [$ id] = $ image;
$ This-> save ();
}
/**
* Modify the items in the shopping cart
*
* @ Param int $ id product number
* @ Param int $ count number of items
* @ Param int $ flag modify type 0: Add 1: subtract 2: Modify 3: clear
* @ Return if the modification fails, false is returned.
*/
Public function modifycart ($ id, $ count, $ flag = ""){
$ Tmpid = $ id;
$ This-> cartarray = $ this-> cartview (); // reads data and writes it to an array.
$ Tmparray = & $ this-> cartarray; // reference
If (! Is_array ($ tmparray [0]) return false;
If ($ id <1 ){
Return false;
}
Foreach ($ tmparray [0] as $ item ){
If ($ item ===$ tmpid ){
Switch ($ flag ){
Case 0: // add quantity. generally, $ count is 1.
$ Tmparray [5] [$ id] + = $ count;
Break;
Case 1: // reduce the number
$ Tmparray [5] [$ id]-= $ count;
Break;
Case 2: // modify the quantity
If ($ count = 0 ){
Unset ($ tmparray [0] [$ id]);
Unset ($ tmparray [1] [$ id]);
Unset ($ tmparray [2] [$ id]);
Unset ($ tmparray [3] [$ id]);
Unset ($ tmparray [4] [$ id]);
Unset ($ tmparray [5] [$ id]);
Unset ($ tmparray [6] [$ id]);
Break;
} Else {
$ Tmparray [5] [$ id] = $ count;
Break;
}
Case 3: // clear items
Unset ($ tmparray [0] [$ id]);
Unset ($ tmparray [1] [$ id]);
Unset ($ tmparray [2] [$ id]);
Unset ($ tmparray [3] [$ id]);
Unset ($ tmparray [4] [$ id]);
Unset ($ tmparray [5] [$ id]);
Unset ($ tmparray [6] [$ id]);
Break;
Default:
Break;
}
}
}
$ This-> save ();
}
/**
* Clear the shopping cart
*
*/
Public function removeall (){
$ This-> cartarray = array ();
$ This-> save ();
}
/**
* View Shopping Cart Information
*
* @ Return array returns a two-dimensional array.
*/
Public function cartview (){
$ Cookie = strips tutorial lashes ($ _ cookie ['cartapi ']);
If (! $ Cookie) return false;
$ Tmpunserialize = unserialize ($ cookie );
Return $ tmpunserialize;
}
/**
* Check whether there are items in the shopping cart.
*
* @ Return bool returns true if there is a commodity; otherwise, false
*/
Public function checkcart (){
$ Tmparray = $ this-> cartview ();
If (count ($ tmparray [0]) <1 ){
Return false;
}
Return true;
}
/**
* Product Statistics
*
* @ Return array returns a one-dimensional array $ arr [0]: total price of product 1 $ arr [1: total price of product 2 $ arr [2]: total price of product 3 $ arr [3]: total number of products
*/
Public function countprice (){
$ Tmparray = $ this-> cartarray = $ this-> cartview ();
$ Outarray = array (); // one-dimensional array
// 0 is the total price of product 1
// 1 is the total price of product 2
// 2 is the total price of product 3
// 3 is the total number of products
$ I = 0;
If (is_array ($ tmparray [0]) {
Foreach ($ tmparray [0] as $ key => $ val ){
$ Outarray [0] + = $ tmparray [2] [$ key] * $ tmparray [5] [$ key];
$ Outarray [1] + = $ tmparray [3] [$ key] * $ tmparray [5] [$ key];
$ Outarray [2] + = $ tmparray [4] [$ key] * $ tmparray [5] [$ key];
$ Outarray [3] + = $ tmparray [5] [$ key];
$ I ++;
}
}
Return $ outarray;
}
/**
* Counting commodity quantity
*
* @ Return int
*/
Public function cartcount (){
$ Tmparray = $ this-> cartview ();
$ Tmpcount = count ($ tmparray [0]);
$ This-> cartcount = $ tmpcount;
Return $ tmpcount;
}
/**
* If you do not use the constructor to save a product, this method must be used.
*
*/
Public function save (){
$ Tmparray = $ this-> cartarray;
$ Tmpserialize = serialize ($ tmparray );
Setcookie ("cartapi", $ tmpserialize, time () + $ this-> expires );
}
/**
* Check whether the shopping cart item exists
*
* @ Param int $ id
* @ Return bool if true exists, otherwise false
*/
Private function checkitem ($ id ){
$ Tmparray = $ this-> cartarray;
If (! Is_array ($ tmparray [0]) return;
Foreach ($ tmparray [0] as $ item ){
If ($ item ===$ id) return true;
}
Return false;
}
}
?>

I hope this article will help you with PHP programming.

Examples in this article describes how php uses cookies to implement shopping cart. Share it with you for your reference. The specific analysis is as follows: p...

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.