Home php教程 PHP源码 PHP购物车类

PHP购物车类

May 25, 2016 pm 05:14 PM
php

因为需求中只记录产品的类型,还有编号..比如食物,,然后什么编号,,这样子.所以没有记录产品的数量..要用的自己改进一下就行了. 

1. [代码][PHP]代码   

<?php

//setcookie("cart","s:26,45,4,4523423|d:17,7,27,26|e:12,13,123,43|f:34"); 
//cartAdd(&#39;e&#39;,&#39;167&#39;); 
//getProInfo(&#39;z&#39;); 
//delProInfo(&#39;f&#39;,&#39;26&#39;); 

/** 
* *********** 
* 用于记录,修改,删除COOKIE的类,用于我的计划的本地信息的更改 
* LIQUAN 
*/ 
class cart 
{ 
/* 
实现COOKIES购物车 
@type 产品类型 
@id 产品编号 
COOKIES的结果为 type1:3,45,23|type2:34,234,34|type3:344,124 
*/ 
function cartAdd(type,id) 
{ 
//如果第一次使用,则重新设置COOKIES 
if(!isset(_COOKIE["cart"])) 
{ 
setcookie("cart",type.":".id); 
} 
else 
{ 
//如果不是第一次,则先取出来 
cartStr=_COOKIE["cart"]; 
//把结果分开 
cartarray=split("\|",cartStr); 

//用于判断类型是否存在,初始不存在,为零 
istype=0; 
//产品编号列表 
id_list; 
//循环产品 
for (i=0;i
{ 
//获取产品名和产品编号列表 
list(carttype,product)=split(":",cartarray); 
//如果已经存在的产品与要保存的相如,把isType改为1,说明产品已经存在 
if (type==carttype) 
{ 
istype=1; 
//获取产品的编号列表 
id_list=product; 
//中断循环 
break ; 
} 
} 
//如果产品不存在 
if(istype==0) 
{ 
//往COOKIES里面加入新产品和产品的编吃 
cartStr=cartStr."|".type.":".id; 
} 
else 
{ 
//把产品列表分开成数组 
id_list=split("\,",id_list); 
//标识产口编号是否存在,初始不存在 
isId=0; 
//循环产品编号 
for(i=0;i
{ 
//如果要保存的产品编号已尼存在 
if(id==id_list) 
{ 
//标识已存在.退出循环 
isId=1; 
break; 
} 
} 
//这里只做产品类型和产品编号,所以相同的不加数量,直接不外理 
//如果相关类型的产品编号不存在,则加上新的编号 
if(isId!=1) 
{ 
cartStr=str_replace(type.":",type.":".id.",",cartStr); 
//cartStr=str_replace(&#39;d:&#39;,&#39;d:2,&#39;,cartStr); 
} 

} 
setcookie("cart",cartStr); 
print_r(_COOKIE["cart"]); 

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


} 
return ; 
} 
//返回相应的产品列表 
/* 
@type产品类型 
*/ 
function getProInfo(type) 
{ 
//获取购物车COOKIES 
cartStr=_COOKIE["cart"]; 

//匹配COOKIES字符,获取产品列表 
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 pro_list; 
// print_r(pro_list); 
} 
/* 
*删除产品 
*@type产品类型名 
*@id 产品编号 
*/ 
function delProInfo(type,id) 
{ 
//获取购物车COOKIES 
cartStr=_COOKIE["cart"]; 

//匹配COOKIES字符,获取产品列表 
preg_str=type."(\d+),)*(\d+)"; 
//echo "
"; 
preg_match("/".preg_str."/i",cartStr,proStr); 

//只有能找到要删除的类型,才进行下面操做 
if(proStr) 
{ 
//查询删除的类型里面是否有要删除的ID 
isproId=strstr(proStr[0],id); 

//如果有,才进行下面操作 
if(isproId) 
{ 
//更新后的产品字符串 
upproStr; 
//更新后的所有最新的产品COOKIES字符 
newCartStr; 

//查询要删除的产品是否有多个 
preg_match("/,/",proStr[0],isend); 
//如果有,进行下面操作 
if(isend) 
{ 
//查询要删除的产品后面是否还有产品 
preg_match("/".id.",/",proStr[0],isdot); 
//如果有,删除后面的逗号 
if(isdot) 
{ 
upproStr=str_replace(id.",","",proStr[0]); 
} 
//如果没有,删除前面的逗号 
else 
{ 
upproStr=str_replace(",".id,"",proStr[0]); 
} 
//更新要插入COOKIES里面的字符串 
newCartStr=str_replace(proStr[0],upproStr,cartStr); 
} 
//如果没有多个,则直接删除该类产品 
else 
{ 
//判断该类产品后面还有没有产品 
preg_match("/".proStr[0]."\|/",cartStr,issu); 
//如果有,则删除后面的分格符 
if(issu) 
{ 
newCartStr=str_replace(proStr[0]."|","",cartStr); 
} 
//如果没有,则删除前面的分格符 
else 
{ 
newCartStr=str_replace("|".proStr[0],"",cartStr); 
} 
} 

setcookie("cart",newCartStr); 

} 
} 
} 
function deletecart() 
{ 
setcookie("cart","",time()-100); 
} 
} 
?>
Copy after login

                   

                   

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles