A detailed tutorial on PHPsession shopping cart-php

Source: Internet
Author: User
Tags php session php foreach
This article mainly introduces PHPsession to implement the shopping cart function, which has some reference value, interested friends can refer to this article for details about the PHP session shopping cart function, which has some reference value. interested friends can refer

In the wamp environment, use PHP session control to complete the effect of the shopping cart. The data is stored in the array and the database is not connected. The effect is good and easy to understand. The following is the code of each part.

Common. php


 1, 'IMG '=> 'IMG/1.jpg', 'title' => 'Xiaomi mobile power 5000mAh ', 'price' => 49 ), array ('id' => 2, 'IMG '=> 'IMG/2.jpg', 'title' => '20000mah XiaoMi mobile power 2 ', 'price' => 149), array ('id' => 3, 'IMG '=> 'IMG/3.jpg ', 'title' => 'millet ring iron earphone Pro', 'price' => 129), array ('id' => 4, 'IMG '=> 'IMG/4.jpg', 'title' => 'millet electric scooter ', 'price' => 1999), array ('id' => 5, 'IMG '=> 'IMG/5.jpg', 'title' => 'Xiaomi notebook ', 'price' => 3499), array ('id' => 6, 'IMG '=> 'IMG/6.jpg', 'title' => 'meters LED smart desk lamp ', 'price' => 169), array ('id' => 7, 'IMG '=> 'IMG/7.jpg', 'title' => 'millet weight', 'price' => 99), array ('id' => 8, 'IMG '=> 'IMG/8.png', 'title' => 'Xiaomi TV 3 s 48 inch ', 'price' => 2599 ));

Index. php


   
   Product Display    
   
  

My shopping cart ">

    $ Value):?>
  • "Width =" 150 "height =" 150 "alt =" XiaoMi mobile power 5000 mAh ">

    "Rel =" external nofollow "> Add to shopping cart

Action. php


 $ Value) {if ($ id = $ value ['id']) {$ arrData = $ arrPro [$ key] ;}} // store product information with a new two-dimensional array $ 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; // determine whether a SESSION exists. if yes, add it after the array. if no SESSION exists, save if (empty ($ _ SESSION ['shopar ']). {$ _ SESSION ['shopcar '] = $ ArrDatax; header ('Location: index. php ');} else {// after the first shopping, the shopping // Is Retrieved again to prevent data from overwriting $ 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']; $ arrD Ataz [3] [$ arrData ['id'] = $ arrData ['price']; $ arrDataz [4] [$ arrData ['id'] = 1; $ _ SESSION ['shopcar '] = $ arrDataz; header ('Location: index. php') ;}} else {echo "no items in the cart! ";}

Spcar. php


 $ Value) {if ($ id = $ value ['id']) {$ arrData = $ arrPro [$ key] ;}} // store product information with a new two-dimensional array $ 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; // determine whether a SESSION exists. if yes, add it after the array. if no SESSION exists, save if (empty ($ _ SESSION ['shopar ']). {$ _ SESSION ['shopcar '] = $ ArrDatax; header ('Location: index. php ');} else {// after the first shopping, the shopping // Is Retrieved again to prevent data from overwriting $ 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']; $ arrD Ataz [3] [$ arrData ['id'] = $ arrData ['price']; $ arrDataz [4] [$ arrData ['id'] = 1; $ _ SESSION ['shopcar '] = $ arrDataz; header ('Location: index. php') ;}} else {echo "no items in the cart! ";}

Delete. php


 

The above is a detailed description of the PHP session instance tutorial for implementing the shopping cart. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.