Home Backend Development PHP Tutorial How to implement shopping cart function in PHP session

How to implement shopping cart function in PHP session

Jul 17, 2020 pm 05:17 PM
php session shopping cart

How to implement shopping cart function in PHP session

In the wamp environment, use PHP session session control to complete the shopping cart effect. The data is stored in an array for practice. There is no connection to the database. The effect is good and simple and easy to understand. The following are each Part of the code

common.php

<?php
header("content-type:text/html;charset=utf-8");
$arrPro = array(
    array(&#39;id&#39;=>1,&#39;img&#39;=>&#39;img/1.jpg&#39;,&#39;title&#39;=>&#39;小米移动电源5000mAh&#39;,&#39;price&#39;=>49),
    array(&#39;id&#39;=>2,&#39;img&#39;=>&#39;img/2.jpg&#39;,&#39;title&#39;=>&#39;20000mAh小米移动电源2&#39;,&#39;price&#39;=>149),
    array(&#39;id&#39;=>3,&#39;img&#39;=>&#39;img/3.jpg&#39;,&#39;title&#39;=>&#39;小米圈铁耳机Pro&#39;,&#39;price&#39;=>129),
    array(&#39;id&#39;=>4,&#39;img&#39;=>&#39;img/4.jpg&#39;,&#39;title&#39;=>&#39;小米家电动滑板车&#39;,&#39;price&#39;=>1999),
    array(&#39;id&#39;=>5,&#39;img&#39;=>&#39;img/5.jpg&#39;,&#39;title&#39;=>&#39;小米笔记本&#39;,&#39;price&#39;=>3499),
    array(&#39;id&#39;=>6,&#39;img&#39;=>&#39;img/6.jpg&#39;,&#39;title&#39;=>&#39;米家LED智能台灯&#39;,&#39;price&#39;=>169),
    array(&#39;id&#39;=>7,&#39;img&#39;=>&#39;img/7.jpg&#39;,&#39;title&#39;=>&#39;小米体重秤&#39;,&#39;price&#39;=>99),
    array(&#39;id&#39;=>8,&#39;img&#39;=>&#39;img/8.png&#39;,&#39;title&#39;=>&#39;小米电视3s 48英寸&#39;,&#39;price&#39;=>2599)
  );
Copy after login

index.php

<?php 
header("content-type:text/html;charset=utf-8");
require &#39;common.php&#39;;
session_start();
$sum = 0;
$class = "";
//判断左上角购物车的样式显示
if(!empty($_SESSION[&#39;shopcar&#39;])){
  $data = $_SESSION[&#39;shopcar&#39;];
  $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=&#39;top&#39;>
  <a href="spcar.php" rel="external nofollow" >我的购物车</a><span class="<?php echo $class;?>"><?php echo $sum;?></span>
  <em class=&#39;star&#39;>★</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[&#39;img&#39;];?>" width="150" height="150" alt="小米移动电源5000mAh"></a> 
    </p> 
    <h3 class="title"> 
    <a href=""><?php echo $value[&#39;title&#39;];?></a>
    </h3> 
    <p class="price"><span class="num">¥<?php echo $value[&#39;price&#39;];?></span></p>
    <p class=&#39;cart&#39;><a href="action.php?id=<?php echo $value[&#39;id&#39;];?>" rel="external nofollow" >加入购物车</a></p>
  </li>
  <?php endforeach;?>
  </ul>
  <p style=&#39;clear:both&#39;></p>
  </section>
</body>
</html>
Copy after login

action.php

<?php
if(!empty($_GET[&#39;id&#39;])){
  require &#39;common.php&#39;;
  session_start();
  $id = $_GET[&#39;id&#39;];
  //把所选ID的商品信息遍历出来
  foreach ($arrPro as $key => $value) {
    if($id == $value[&#39;id&#39;]){
    $arrData = $arrPro[$key];
    }
  }

  //用一个新的二维数组把商品信息存起来
  $arrDatax[0][$arrData[&#39;id&#39;]] = $arrData[&#39;id&#39;];
  $arrDatax[1][$arrData[&#39;id&#39;]] = $arrData[&#39;img&#39;];
  $arrDatax[2][$arrData[&#39;id&#39;]] = $arrData[&#39;title&#39;];
  $arrDatax[3][$arrData[&#39;id&#39;]] = $arrData[&#39;price&#39;];
  $arrDatax[4][$arrData[&#39;id&#39;]] = 1;
  //判断是否有SESSION存在,有则在数组后添加,没有则直接存
  if(empty($_SESSION[&#39;shopcar&#39;])){
    $_SESSION[&#39;shopcar&#39;] = $arrDatax;
    header(&#39;Location:index.php&#39;);
  }else{
    //第一次购物之后的购物
    //重新取出来,防止数据覆盖
    $arrDataz = $_SESSION[&#39;shopcar&#39;];
    if(in_array($id,$arrDataz[0])){
      $arrDataz[4][$arrData[&#39;id&#39;]] += 1;
      $_SESSION[&#39;shopcar&#39;] = $arrDataz;
      header(&#39;Location:index.php&#39;);
    }else{
      $arrDataz[0][$arrData[&#39;id&#39;]] = $arrData[&#39;id&#39;];
      $arrDataz[1][$arrData[&#39;id&#39;]] = $arrData[&#39;img&#39;];
      $arrDataz[2][$arrData[&#39;id&#39;]] = $arrData[&#39;title&#39;];
      $arrDataz[3][$arrData[&#39;id&#39;]] = $arrData[&#39;price&#39;];
      $arrDataz[4][$arrData[&#39;id&#39;]] = 1;
      $_SESSION[&#39;shopcar&#39;] = $arrDataz;
      header(&#39;Location:index.php&#39;);
    }

  }
}else{
  echo "购物车没有商品!";
}
Copy after login
Copy after login

spcar.php

<?php
if(!empty($_GET[&#39;id&#39;])){
  require &#39;common.php&#39;;
  session_start();
  $id = $_GET[&#39;id&#39;];
  //把所选ID的商品信息遍历出来
  foreach ($arrPro as $key => $value) {
    if($id == $value[&#39;id&#39;]){
    $arrData = $arrPro[$key];
    }
  }

  //用一个新的二维数组把商品信息存起来
  $arrDatax[0][$arrData[&#39;id&#39;]] = $arrData[&#39;id&#39;];
  $arrDatax[1][$arrData[&#39;id&#39;]] = $arrData[&#39;img&#39;];
  $arrDatax[2][$arrData[&#39;id&#39;]] = $arrData[&#39;title&#39;];
  $arrDatax[3][$arrData[&#39;id&#39;]] = $arrData[&#39;price&#39;];
  $arrDatax[4][$arrData[&#39;id&#39;]] = 1;
  //判断是否有SESSION存在,有则在数组后添加,没有则直接存
  if(empty($_SESSION[&#39;shopcar&#39;])){
    $_SESSION[&#39;shopcar&#39;] = $arrDatax;
    header(&#39;Location:index.php&#39;);
  }else{
    //第一次购物之后的购物
    //重新取出来,防止数据覆盖
    $arrDataz = $_SESSION[&#39;shopcar&#39;];
    if(in_array($id,$arrDataz[0])){
      $arrDataz[4][$arrData[&#39;id&#39;]] += 1;
      $_SESSION[&#39;shopcar&#39;] = $arrDataz;
      header(&#39;Location:index.php&#39;);
    }else{
      $arrDataz[0][$arrData[&#39;id&#39;]] = $arrData[&#39;id&#39;];
      $arrDataz[1][$arrData[&#39;id&#39;]] = $arrData[&#39;img&#39;];
      $arrDataz[2][$arrData[&#39;id&#39;]] = $arrData[&#39;title&#39;];
      $arrDataz[3][$arrData[&#39;id&#39;]] = $arrData[&#39;price&#39;];
      $arrDataz[4][$arrData[&#39;id&#39;]] = 1;
      $_SESSION[&#39;shopcar&#39;] = $arrDataz;
      header(&#39;Location:index.php&#39;);
    }

  }
}else{
  echo "购物车没有商品!";
}
Copy after login
Copy after login

delete.php

<?php
session_start();
if(!empty($_GET[&#39;id&#39;])){
  $arrData = $_SESSION[&#39;shopcar&#39;];
  //判断对应的商品ID信息
    if(in_array($_GET[&#39;id&#39;],$arrData[0])){
      unset($arrData[0][$_GET[&#39;id&#39;]]);
      unset($arrData[1][$_GET[&#39;id&#39;]]);
      unset($arrData[2][$_GET[&#39;id&#39;]]);
      unset($arrData[3][$_GET[&#39;id&#39;]]);
      unset($arrData[4][$_GET[&#39;id&#39;]]);
      $_SESSION[&#39;shopcar&#39;] = $arrData;
    }
    header(&#39;Location:spcar.php&#39;);
}
Copy after login

Related learning recommendations: PHP Programming from beginner to master

The above is the detailed content of How to implement shopping cart function in PHP session. For more information, please follow other related articles on the PHP Chinese website!

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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 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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

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.

See all articles