PHP Shopping Cart class:
Because only the type of the product is recorded in the requirement, there is also the number ... Like food, and then what number, this looks like. So there is no record of the number of products. You need to use the improvement on the line.
Setcookie ("Cart", "s:26,45,4,4523423|d:17,7,27,26|e:12,13,123,43|f:34");
Cartadd (' e ', ' 167 ');
Getproinfo (' z ');
Delproinfo (' F ', ' 26 ');
/**
* ***********
* Class for recording, modifying, deleting cookies, changes to local information for my plan
* Liquan
*/
Class Cart
{
/*
Realization of cookies Shopping cart
@type Product Type
@id Product number
The result of cookies is type1:3,45,23|type2:34,234,34|type3:344,124
*/
function Cartadd (type,id)
{
If you use it for the first time, reset the cookies
if (!isset (_cookie["cart"))
{
Setcookie ("cart", type. ":". id);
}
Else
{
If it's not the first time, take it out first.
cartstr=_cookie["cart"];
Separate the results.
Cartarray=split ("|", CARTSTR);
Used to determine whether a type exists, does not exist initially, is zero
istype=0;
Product Number List
Id_list;
Recycling Products
for (i=0;i
{
Get product name and product number list
List (carttype,product) =split (":", Cartarray[i]);
If the existing product and the Hsiang to be saved, change the Istype to 1, indicating that the product already exists
if (Type==carttype)
{
istype=1;
Get a numbered list of products
Id_list=product;
Interrupt loop
break;
}
}
If the product does not exist
if (istype==0)
{
Add new products and products to cookies
Cartstr=cartstr. "|". Type. ":". Id;
}
Else
{
Separate the product list into groups
Id_list=split (",", id_list);
Identifies whether the yield number exists and does not initially exist
Isid=0;
Recycle Product number
for (i=0;i
{
If the product number you want to save already exists
if (Id==id_list[i])
{
Identity already exists. Exit loop
isid=1;
Break
}
}
Here only to do product type and product number, so the same no amount, directly ignore
If the product number of the related type does not exist, add a new number
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 to the appropriate product list
/*
@type Product Type
*/
function Getproinfo (type)
{
Get Shopping cart Cookies
cartstr=_cookie["cart"];
Match cookies character, get 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);
Back to Product List
return pro_list;
Print_r (pro_list);
}
/*
* Delete Product
* @type Product Type name
* @id Product number
*/
function Delproinfo (type,id)
{
Get Shopping cart Cookies
cartstr=_cookie["cart"];
Match cookies character, get product list
Preg_str=type. ":( (d+),) * (d+) ";
echo "
";
Preg_match ("/". Preg_str.) /i ", cartstr,prostr);
Do the following if you can find the type you want to delete
if (PROSTR)
{
Is there an ID to delete in the type of query deletion?
Isproid=strstr (Prostr[0],id);
If so, do the following
if (isproid)
{
Updated product string
UPPROSTR;
Updated cookies characters for all the latest products
NEWCARTSTR;
Query whether the product you want to delete has more than one
Preg_match ("/,/", prostr[0],isend);
If so, do the following
if (isend)
{
Find out if there is a product behind the product you want to delete
Preg_match ("/". Id. ",/", Prostr[0],isdot);
If so, delete the following 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 inside the cookie
Newcartstr=str_replace (PROSTR[0],UPPROSTR,CARTSTR);
}
If there are not more than one, delete the product directly
Else
{
Determine if there are any products behind this product
Preg_match ("/". prostr[0]. "| /", CARTSTR,ISSU);
If it does, delete the following separator
if (ISSU)
{
Newcartstr=str_replace (prostr[0]. "|", "", cartstr);
}
If not, delete the preceding separator
Else
{
Newcartstr=str_replace ("|"). Prostr[0], "", cartstr);
}
}
Setcookie ("cart", newcartstr);
}
}
}
function Deletecart ()
{
Setcookie ("Cart", "", Time ()-100);
}
}
?>