PHP session實現(xiàn)購物車功能
在wamp環(huán)境下,用PHP的session會話控制完成購物車的效果,數(shù)據(jù)存放在數(shù)組里練習(xí),沒有連接數(shù)據(jù)庫,效果不錯,簡單易懂,以下是各部分的代碼
common.php
<?php header("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> <div class='top'> <a href="spcar.php" rel="external nofollow" >我的購物車</a><span class="<?php echo $class;?>"><?php echo $sum;?></span> <em class='star'>★</em> </div> <ul class="list"> <?php foreach ($arrPro as $key => $value):?> <li> <div class="figure"> <a href=""><img src=" rel="external nofollow" <?php echo $value['img'];?>" width="150" height="150" alt="小米移動電源5000mAh"></a> </div> <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> <div style='clear:both'></div> </section> </body> </html>
action.php
<?php if(!empty($_GET['id'])){ require 'common.php'; session_start(); $id = $_GET['id']; //把所選ID的商品信息遍歷出來 foreach ($arrPro as $key => $value) { if($id == $value['id']){ $arrData = $arrPro[$key]; } } //用一個新的二維數(shù)組把商品信息存起來 $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存在,有則在數(shù)組后添加,沒有則直接存 if(empty($_SESSION['shopcar'])){ $_SESSION['shopcar'] = $arrDatax; header('Location:index.php'); }else{ //第一次購物之后的購物 //重新取出來,防止數(shù)據(jù)覆蓋 $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
<?php if(!empty($_GET['id'])){ require 'common.php'; session_start(); $id = $_GET['id']; //把所選ID的商品信息遍歷出來 foreach ($arrPro as $key => $value) { if($id == $value['id']){ $arrData = $arrPro[$key]; } } //用一個新的二維數(shù)組把商品信息存起來 $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存在,有則在數(shù)組后添加,沒有則直接存 if(empty($_SESSION['shopcar'])){ $_SESSION['shopcar'] = $arrDatax; header('Location:index.php'); }else{ //第一次購物之后的購物 //重新取出來,防止數(shù)據(jù)覆蓋 $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
<?php session_start(); if(!empty($_GET['id'])){ $arrData = $_SESSION['shopcar']; //判斷對應(yīng)的商品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'); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iis訪問出現(xiàn)各種問題(Vs訪問正常)的部分處理方法詳細整理
在vs中調(diào)試都可以正常,但是在iis訪問就會出現(xiàn)各種問題,很是疑惑索性把這些問題整理一下,這樣更容易的可以處理方法說明清楚,感興趣的朋友可以了解下2013-01-01.Net Core 之 Ubuntu 14.04 部署過程(圖文詳解)
本篇文章主要介紹了.Net Core 之 Ubuntu 14.04 部署過程(圖文詳解),有興趣的可以了解一下。2016-11-11asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼實例
這篇文章主要介紹了asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼,結(jié)合實例形式詳細分析了asp.net基于QRCode生成二維碼的具體實現(xiàn)技巧,需要的朋友可以參考下2016-06-06ASP.net?core使用Autofac實現(xiàn)泛型依賴注入
這篇文章主要介紹了ASP.net?core使用Autofac實現(xiàn)泛型依賴注入的方式學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04MVC 5 第一章 創(chuàng)建MVC 5 web應(yīng)用程序
本章將講述一些構(gòu)建ASP.NET MVC 5 web application的一些基礎(chǔ)知識, 通過本章學(xué)習(xí),你應(yīng)該能夠掌握到構(gòu)建MVC 5應(yīng)用程序的基本步驟,并且通過展示一個完整的MVC 5 hello world應(yīng)用程序了解MVC 5應(yīng)用程序所帶來的用戶體驗。2014-06-06