PHP shopping cart
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 );
}
}
?>