概述PHP session如何实现购物车功能
在wamp环境下,用PHP的session会话控制完成购物车的效果,数据存放在数组里练习,没有连接数据库,效果不错,简单易懂,以下是各部分的代码
common.PHP
<?PHPheader("content-type:text/HTML;charset=utf-8");$arrPro = array( array('ID'=>1,'img'=>'img/1.jpg','Title'=>'小米移动电源5000mAh','price'=>49), array('ID'=>2,'img'=>'img/2.jpg','Title'=>'20000mAh小米移动电源2','price'=>149), array('ID'=>3,'img'=>'img/3.jpg','Title'=>'小米圈铁耳机Pro','price'=>129), array('ID'=>4,'img'=>'img/4.jpg','Title'=>'小米家电动滑板车','price'=>1999), array('ID'=>5,'img'=>'img/5.jpg','Title'=>'小米笔记本','price'=>3499), array('ID'=>6,'img'=>'img/6.jpg','Title'=>'米家LED智能台灯','price'=>169), array('ID'=>7,'img'=>'img/7.jpg','Title'=>'小米体重秤','price'=>99), array('ID'=>8,'img'=>'img/8.png','Title'=>'小米电视3s 48英寸','price'=>2599) );
index.PHP
<?PHP header("content-type:text/HTML;charset=utf-8");require 'common.PHP';session_start();$sum = 0;$class = "";//判断左上角购物车的样式显示if(!empty($_SESSION['shopcar'])){ $data = $_SESSION['shopcar']; $sum = array_sum($data[4]); $class = "on"; //右上角圆点 if(empty($data[0])){ $class = ""; } }?><!DOCTYPE HTML><HTML lang="en"><head> <Meta charset="UTF-8"> <Title>商品展示</Title> <style type="text/CSS"> section{ wIDth:1032px; height: 700px; margin: 40px auto; } .top{ float: right; position: relative; wIDth: 190px; height: 34px; border: 1px solID #ccc; margin-right: 32px; text-align: center; line-height: 34px; border-radius: 4px; Transition: all .3s linear; -moz-Transition: all .3s linear; -webkit-Transition: all .3s linear; } .top a{ color: #d00; } .top:hover{ wIDth: 210px; color: #fff; Font-weight: bold; background-color: #d00; border-radius: 6px; } .top:hover a{ color: #fff; } .top:hover span{ background-color: #fff; color: #d00; } .top:hover .star{ right:150px; top: 0; Font-size: 14px; color: #ff0; transform: rotate(1080deg); } .top span{ position: absolute; top:2px; right: 40px; wIDth: 18px; height: 18px; Font-weight: bold; border-radius: 9px; line-height: 18px; text-align: center; Font-size: 12px; Font-weight: border; color: #fff; } .star{ color: #fff; Font-size: 48px; Font-style: normal; position: absolute; right:530px; top:186px; transform: rotate(60deg); Transition: all .3s ease; } .on{ background-color: #e00; } .List{ margin: 20px 20px; padding: 36px 0; List-style: none; } a{ display: block; color: #757575; text-decoration: none; } .List li{ float: left; height: 246px; wIDth: 234px; padding: 10px 0 20px; margin-right:12px; margin-top: 20px; border: 1px solID #ccc; background: #fff; -webkit-Transition: all .2s linear; Transition: all .2s linear; } .List li:hover{ Box-shadow: 2px 4px 5px #aaa; } .figure{ wIDth: 150px; height: 150px; margin: 0 auto 18px; } .Title{ color: #222; Font-size: 14px; Font-weight: normal; text-align: center; } .price{ margin: 0 10px 10px; text-align: center; color: #ff6700; } .cart{ margin: 0 15px 5px; text-align: center; } .cart a{ color: #a34; wIDth: 190px; height: 24px; border-radius: 4px; margin: 0 8px 5px; text-align: center; } .cart a:hover{ color: #eee; Box-shadow: 0 2px 1px #333,0 2px 1px #666; background-color: #ccc; background-image: linear-gradIEnt(#33a6b8,#0089a7) } .num{ text-align: center; color: #ff6700; } </style></head><body> <section> <p class='top'> <a href="spcar.PHP" rel="external nofollow" >我的购物车</a><span class="<?PHP echo $class;?>"><?PHP echo $sum;?></span> <em class='star'>★</em> </p> <ul class="List"> <?PHP foreach ($arrPro as $key => $value):?> <li> <p class="figure"> <a href=""><img src=" rel="external nofollow" <?PHP echo $value['img'];?>" wIDth="150" height="150" alt="小米移动电源5000mAh"></a> </p> <h3 class="Title"> <a href=""><?PHP echo $value['Title'];?></a> </h3> <p class="price"><span class="num">¥<?PHP echo $value['price'];?></span></p> <p class='cart'><a href="action.PHP?ID=<?PHP echo $value['ID'];?>" rel="external nofollow" >加入购物车</a></p> </li> <?PHP endforeach;?> </ul> <p style='clear:both'></p> </section></body></HTML>
action.PHP
<?PHPif(!empty($_GET['ID'])){ require 'common.PHP'; session_start(); $ID = $_GET['ID']; //把所选ID的商品信息遍历出来 foreach ($arrPro as $key => $value) { if($ID == $value['ID']){ $arrData = $arrPro[$key]; } } //用一个新的二维数组把商品信息存起来 $arrdatax[0][$arrData['ID']] = $arrData['ID']; $arrdatax[1][$arrData['ID']] = $arrData['img']; $arrdatax[2][$arrData['ID']] = $arrData['Title']; $arrdatax[3][$arrData['ID']] = $arrData['price']; $arrdatax[4][$arrData['ID']] = 1; //判断是否有SESSION存在,有则在数组后添加,没有则直接存 if(empty($_SESSION['shopcar'])){ $_SESSION['shopcar'] = $arrdatax; header('Location:index.PHP'); }else{ //第一次购物之后的购物 //重新取出来,防止数据覆盖 $arrDataz = $_SESSION['shopcar']; if(in_array($ID,$arrDataz[0])){ $arrDataz[4][$arrData['ID']] += 1; $_SESSION['shopcar'] = $arrDataz; header('Location:index.PHP'); }else{ $arrDataz[0][$arrData['ID']] = $arrData['ID']; $arrDataz[1][$arrData['ID']] = $arrData['img']; $arrDataz[2][$arrData['ID']] = $arrData['Title']; $arrDataz[3][$arrData['ID']] = $arrData['price']; $arrDataz[4][$arrData['ID']] = 1; $_SESSION['shopcar'] = $arrDataz; header('Location:index.PHP'); } }}else{ echo "购物车没有商品!";}
spcar.PHP
<?PHPif(!empty($_GET['ID'])){ require 'common.PHP'; session_start(); $ID = $_GET['ID']; //把所选ID的商品信息遍历出来 foreach ($arrPro as $key => $value) { if($ID == $value['ID']){ $arrData = $arrPro[$key]; } } //用一个新的二维数组把商品信息存起来 $arrdatax[0][$arrData['ID']] = $arrData['ID']; $arrdatax[1][$arrData['ID']] = $arrData['img']; $arrdatax[2][$arrData['ID']] = $arrData['Title']; $arrdatax[3][$arrData['ID']] = $arrData['price']; $arrdatax[4][$arrData['ID']] = 1; //判断是否有SESSION存在,有则在数组后添加,没有则直接存 if(empty($_SESSION['shopcar'])){ $_SESSION['shopcar'] = $arrdatax; header('Location:index.PHP'); }else{ //第一次购物之后的购物 //重新取出来,防止数据覆盖 $arrDataz = $_SESSION['shopcar']; if(in_array($ID,$arrDataz[0])){ $arrDataz[4][$arrData['ID']] += 1; $_SESSION['shopcar'] = $arrDataz; header('Location:index.PHP'); }else{ $arrDataz[0][$arrData['ID']] = $arrData['ID']; $arrDataz[1][$arrData['ID']] = $arrData['img']; $arrDataz[2][$arrData['ID']] = $arrData['Title']; $arrDataz[3][$arrData['ID']] = $arrData['price']; $arrDataz[4][$arrData['ID']] = 1; $_SESSION['shopcar'] = $arrDataz; header('Location:index.PHP'); } }}else{ echo "购物车没有商品!";}
delete.PHP
<?PHPsession_start();if(!empty($_GET['ID'])){ $arrData = $_SESSION['shopcar']; //判断对应的商品ID信息 if(in_array($_GET['ID'],$arrData[0])){ unset($arrData[0][$_GET['ID']]); unset($arrData[1][$_GET['ID']]); unset($arrData[2][$_GET['ID']]); unset($arrData[3][$_GET['ID']]); unset($arrData[4][$_GET['ID']]); $_SESSION['shopcar'] = $arrData; } header('Location:spcar.PHP');}
相关学习推荐:PHP编程从入门到精通
总结
以上是内存溢出为你收集整理的PHP session如何实现购物车功能全部内容,希望文章能够帮你解决PHP session如何实现购物车功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
© 版权声明
THE END
请登录后查看评论内容