Home  >  Article  >  Backend Development  >  PHP Shopping Cart Class_PHP Tutorial

PHP Shopping Cart Class_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:07981browse

PHP shopping cart class

PHP shopping cart class:

Because the requirements only record the type of product, and the number. For example, food, and then what number, like this. Therefore, the quantity of the product is not recorded. If you want to use it, you can just improve it yourself.

//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, and deleting COOKIE, for changes in local information of my plan

 * LIQUAN

*/

class cart

 {

 /*

Implement 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 used for the first time, reset 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 the type exists. If it does not exist initially, it will be zero

istype=0;

//Product number list

id_list;

// Recycling products

for (i=0;i

 {

//Get the product name and product number list

list(carttype,product)=split(":",cartarray[i]);

//If the existing product is similar to the one to be saved, change isType to 1, indicating that the product already exists

 if (type==carttype)

 {

istype=1;

//Get the product number list

id_list=product;

//Interrupt loop

break ;

 }

 }

//If the product does not exist

 if(istype==0)

 {

//Add new products and product codes to COOKIES

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

 }

else

 {

// Separate the product list into arrays

 id_list=split(",",id_list);

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

isId=0;

//Cycle product number

for(i=0;i

 {

//If the product number to be saved already exists

 if(id==id_list[i])

 {

//The logo already exists. Exit the loop

isId=1;

break;

 }

 }

//Only the product type and product number are shown here, so the same ones do not add the quantity and are ignored directly

//If the product number of the relevant 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 corresponding product list

 /*

 @type product type

 */

Function getProInfo(type)

 {

//Get shopping cart COOKIES

cartStr=_COOKIE["cart"];

// Match COOKIES characters and get 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 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 characters and get the product list

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

 //echo "
";

preg_match("/".preg_str."/i",cartStr,proStr);

//Only perform the following operations if you can find the type you want to delete

 if(proStr)

 {

//Query whether there is an ID to be deleted in the deleted type

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

//If yes, then proceed with the following operations

 if(isproId)

 {

//Updated product string

upproStr;

//Updated with all the latest product COOKIES characters

newCartStr;

// Check whether there are multiple products to be deleted

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

//If yes, perform the following operations

 if(isend)

 {

// Check if there are any products behind the product to be deleted

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

//If there is, 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 be inserted into COOKIES

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

 }

//If there are not more than one, delete the product directly

else

 {

//Judge whether there are other products in this category

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

//If there is one, delete the following separator character

 if(issu)

 {

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

 }

//If not, delete the previous separator character

else

 {

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

 }

 }

setcookie("cart",newCartStr);

 }

 }

 }

Function deletecart()

 {

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

 }

 }

 ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/845130.htmlTechArticlePHP Shopping Cart Class PHP Shopping Cart Class: Because only the product type and number are recorded in the requirement...for example Food, and then what number, it looks like this. So the quantity of the product is not recorded. You have to use it yourself...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn