PHP shopping cart class _ PHP Tutorial

Source: Internet
Author: User
PHP shopping cart class. PHP shopping cart Class PHP shopping cart class: because the requirement only records the product type, there are numbers .. for example, what is the number of the food. so the number of products is not recorded .. PHP shopping cart class to use

PHP shopping cart class:

Because only the product type is recorded in the requirement, and there is a number .. for example, what is the number of the food. so the number of products is not recorded .. you just need to improve it yourself.

  

// Setcookie ("cart", "s: 26,45, 4,4523423 | d: 17,7, 27,26 | e: 12,13, 123,43 | f: 34 ");

// CartAdd ('e', '123 ');

// GetProInfo ('Z ');

// DelProInfo ('F', '26 ');

/**

************

* This class is used to record, modify, and delete cookies. it is used to change the local information of my plan.

* LIQUAN

*/

Class cart

{

/*

Implement COOKIES shopping cart

@ Type product type

@ Id product No.

Cookie result: type1: 34,234, 23 | type2: 344,124, 34 | type3:

*/

Function cartAdd (type, id)

{

// If you use COOKIES for the first time, reset COOKIES.

If (! Isset (_ COOKIE ["cart"])

{

Setcookie ("cart", type. ":". id );

}

Else

{

// If it is not the first time, it is obtained first.

CartStr = _ COOKIE ["cart"];

// Separate the results

Cartarray = split ("\ |", cartStr );

// Used to determine whether the type exists. it does not exist initially and is zero.

Istype = 0;

// Product ID list

Id_list;

// Cyclic product

For (I = 0; I

{

// Obtain the product name and product ID list

List (carttype, product) = split (":", cartarray [I]);

// If the existing product is like the one to save, change isType to 1, indicating that the product already exists

If (type = carttype)

{

Istype = 1;

// Obtain the product ID list

Id_list = product;

// Interrupt loop

Break;

}

}

// If the product does not exist

If (istype = 0)

{

// Add new product and product encoding to COOKIES

CartStr = cartStr. "|". type. ":". id;

}

Else

{

// Split the product list into an array

Id_list = split ("\,", id_list );

// Identify whether the production port number exists and does not exist initially

IsId = 0;

// Cyclic product No.

For (I = 0; I

{

// If the product number to be saved already exists

If (id = id_list [I])

{

// Mark that an existing one exists. exit the loop

IsId = 1;

Break;

}

}

// Only the product type and product number are used here. Therefore, the same number is not added, but cannot be used directly.

// If the product ID of the related type does not exist, add a new ID.

If (isId! = 1)

{

CartStr = str_replace (type. ":", type. ":". id. ",", cartStr );

// CartStr = str_replace ('d: ', 'd: 2,', cartStr );

}

}

Setcookie ("cart", cartStr );

Print_r (_ COOKIE ["cart"]);

// Setcookie ("cart", "", time ()-100 );

}

Return;

}

// Return the corresponding product list

/*

@ Type product type

*/

Function getProInfo (type)

{

// Get shopping cart COOKIES

CartStr = _ COOKIE ["cart"];

// Match COOKIES to obtain the product list

Preg_str = type. ":( (\ d +),) * (\ d + )";

// Echo"
";

Preg_match ("/". preg_str. "/", cartStr, proStr );

// Echo proStr [0];

// Exit ();

List (protype, product) = split (":", proStr [0]);

Return product;

// Pro_list = split (",", product );

// Return to the product list

// Return pro_list;

// Print_r (pro_list );

}

/*

* Delete a product

* @ Type product type name

* @ Id: Product id

*/

Function delProInfo (type, id)

{

// Get shopping cart COOKIES

CartStr = _ COOKIE ["cart"];

// Match COOKIES to obtain the product list

Preg_str = type. ":( (\ d +),) * (\ d + )";

// Echo"
";

Preg_match ("/". preg_str. "/I", cartStr, proStr );

// Perform the following operations only when the type to be deleted can be found

If (proStr)

{

// Query whether the deletion type contains the ID to be deleted.

IsproId = strstr (proStr [0], id );

// If yes, perform the following operations

If (isproId)

{

// Updated product string

UpproStr;

// Updated COOKIES for all the latest products

NewCartStr;

// Query whether there are multiple products to be deleted

Preg_match ("/,/", proStr [0], isend );

// If yes, perform the following operations

If (isend)

{

// Query whether there are products after the product to be deleted

Preg_match ("/". id. ",/", proStr [0], isdot );

// If yes, delete the comma

If (isdot)

{

UpproStr = str_replace (id. ",", "", proStr [0]);

}

// If not, delete the preceding comma

Else

{

UpproStr = str_replace (",". id, "", proStr [0]);

}

// Update the string to insert into COOKIES

NewCartStr = str_replace (proStr [0], upproStr, cartStr );

}

// If there are no more products, delete them directly

Else

{

// Determine whether there are any products behind this product

Preg_match ("/". proStr [0]. "\ |/", cartStr, issu );

// If yes, delete the separator

If (issu)

{

NewCartStr = str_replace (proStr [0]. "|", "", cartStr );

}

// If no, delete the previous shard.

Else

{

NewCartStr = str_replace ("|". proStr [0], "", cartStr );

}

}

Setcookie ("cart", newCartStr );

}

}

}

Function deletecart ()

{

Setcookie ("cart", "", time ()-100 );

}

}

?>

Shopping cart class in PHP: because only the product type is recorded in the requirement, there is also a number .. for example, what is the number of the food. so the number of products is not recorded .. use your own...

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.