Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial PHP cookie +购物车 怎么写??

PHP cookie +购物车 怎么写??

Jun 23, 2016 pm 02:26 PM

现在做网站做到购物车这里我不会写了!!请高手写的代码给参考下!!谢谢了啊


回复讨论(解决方案)

那个不能放cookies的、用户下次登陆找收藏在购物车里的商品一旦没了咋办

ajax+数据库存储内容.

我也正需要购物车的资料呢,2楼所说的AJax+数据库是怎么实现的,有源码的话共享下...谢谢了!

<?php$act=$_GET[act];$product_id=$_GET[prcid];if($act=="add"){ 	$cars_id=explode(",",$_COOKIE[buypro]);	//echo "<pre class="brush:php;toolbar:false">";   	//print_r($cars_id);	//echo"
Copy after login
"; //exit; foreach($cars_id as $pr_id) { if($pr_id==$product_id) { echo "<script>alert('您已经购买了此商品,请查看购物车!');location.href='car.php';</script>"; exit; } } if(empty($_COOKIE[buypro])) { setcookie("buypro",$product_id,time()+3600); setcookie("buycount","1",time()+3600); } else { $newpro=$_COOKIE[buypro].",".$product_id; $newcount=$_COOKIE[buycount].","."1"; setcookie("buypro",$newpro,time()+3600); setcookie("buycount",$newcount,time()+3600); }}//删除if($act=="del"){$buy=$_COOKIE[buypro];$cars_id=explode(",",$buy);$buycountary=explode(",",$_COOKIE[buycount]);$newid=array();$carcount=array();for($i=0;$i

学习中

给你共享一个全套的,接点分,不够用了

//index.php<?phpinclude ("conn.php");$sql="select * from produce";//查询所有商品$rs=mysql_query($sql,$conn);//执行sql语句,得到一个结果集while($row=mysql_fetch_array($rs))//遍历结果集{?><table width="343" height="152" border="1" style="float:left">  <tr>    <td width="124" rowspan="3"><img  src="/static/imghw/default1.png"  data-src="images/<?php echo $row["  class="lazy"pimg"]? alt="PHP cookie +购物车 怎么写??" >" width="123"    style="max-width:90%" border="0" /></td>    <td width="203" height="35">货物名称:<?php echo $row["pname"] ?></td>  </tr>  <tr>    <td height="28">货物价格:<?php echo $row["price"] ?></td>  </tr>  <tr>    <td height="27" align="center"><a href="buy.php?id=<?php echo $row["pid"] ?>&pname=<?phpecho $row["pname"] ?>">购买</a></td>  </tr>  </table><?php}?>//conn.php<?php $conn=mysql_connect("localhost","root","root"); mysql_select_db("shop",$conn); mysql_query("set names utf8");?>//car.php<?php	include_once('public.inc.php');	$arr=$_SESSION["mycar"];//从session中拿出二维数组		?>	<table width="600" height="37" border="1">		<tr>			<td width="96">商品ID</td>			<td width="154">商品数量</td>				<td width="154">商品单价</td>				<td width="154">商品总价</td>			<td width="177">删除</td>		</tr>		<?php		foreach($arr as $a)//遍历这个二维数组		{	$tp=$a["num"]*$a["price"];				$_tp += $tp;		$num += $a["num"];		?>		<tr>			<td width="96"><?php echo $a["pid"] ?></td> 			<td width="154"><?php echo $a["num"] ?></td>				<td width="154"><?php echo $a["price"] ?></td>				<td width="154"><?php echo $tp ?></td>			<td width="177"><a href="delete.php?id=<?php echo $a[pid] ?>">删除</a></td>		</tr>		<?php		}		?>	<tr>			<td width="96"> </td>			<td width="154"><?=$num?></td>				<td width="154">总价</td>				<td width="154"><?php echo $_tp ?></td>			<td width="177">s</td>		</tr>	</table>		</form>		<a href="order.php">提交订单</a>//返回到首页//buy.php<?php	include_once('public.inc.php');		ob_start();//要清空缓存就必须ob_start()		$sql="select * from qf_act where id = $_GET[id]";		$rs=mysql_query($sql);		$row=mysql_fetch_array($rs);		$price=$row["price"];		$pid=$_GET["id"];//得到购买物品的id		$arr=$_SESSION["mycar"];//将session中的变量取出来		//下面先判断这个变量是否是数组,可以得到以前是否买过东西		if(is_array($arr))		{		//如果是数组,说明以前买过东西		//如果买过东西又分两种情况:			  if(array_key_exists($pid,$arr))			  {			  // 1、array_key_exists($pid,$arr)判断$arr中是否存在键值为$pid的一个一维数组,如果存在的话,就说明此商品以前购买过,只需要把数量加1				   $uu=$arr[$pid]; //从二维数组里拿出对应的一维数组,该一维数组包括id name num 三个值				   $uu["num"]=$uu["num"]+1;  //改变数量,将数量加1				   $arr[$pid]=$uu; //改完后再将此一维数组放回二维数组中			  }			  else			  {    //2.此商品第一次购买,就将得到的id和name值组成一个一维数组				   $arr[$pid]=array("pid"=>$pid,"price"=>$price,"num"=>1);			  }		}		else		{		 		$arr[$pid]=array("pid"=>$pid,"price"=>$price,"num"=>1);		}		$_SESSION["mycar"]=$arr; //购买完后,将此数组重新放入session中,便可以在各个页面看到此session		ob_clean();//清空缓存		header("location:car.php");//跳转到购物车界面(car.php)?>
Copy after login


也许不是非常强大,但是做个是个基本功能都OK。根据自己的要求自己可以在修改一下

thinkPHP购物车写法代码怎么写

学习中

未登录用户购物车商品存储到cookie,已登录用户存到数据库

<?php$method=@$_POST['method'];$good_id=@$_POST['id'];$good_name=@$_POST['name'];$good_item=@$_POST['item_code'];$good_num=@$_POST['number'];$good_car_array=unserialize($_COOKIE['good_car_string']);switch($method){	case "add":		if(array_key_exists($good_id,$good_car_array))		{			echo "购物车中已经存在该商品!";		}		else		{			$good_car_array[$good_id]=array($good_item,$good_name,$good_num);			echo "添加成功";		}	break;		case "change": 		$good_car_array[$good_id][2]=$good_num;	break;		case "del":		unset($good_car_array[$good_id]);	break;	 	case "delall": 		$good_car_array="";	break;}setcookie('good_car_string',serialize($good_car_array),time()+3600*30*24);print_r($good_car_array);?>
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles