PHP session implements the shopping cart function and session shopping cart

Source: Internet
Author: User
Tags php session php foreach

PHP session implements the shopping cart function and session shopping cart

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

<? Phpheader ("content-type: text/html; charset = UTF-8"); $ arrPro = array ('id' => 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 headphones Pro ', 'price' => 129), array ('id' => 4, 'img '=> 'img/4.jpg', 'title' => 'xiaomi electric scooter ', 'price' => 1999), array ('id' => 5, 'img '=> 'img/5.jpg', 'title' => 'xiaomi notebook ', 'price' => 3499), array ('id' => 6, 'img '=> 'img/6.jpg', 'title' => 'smart meters LED desk lamp ', 'price' => 169), array ('id' => 7, 'img '=> 'img/7.jpg', 'title' => 'millet scale ', 'price' => 99), array ('id' => 8, 'img '=> 'img/8.png ', 'title' => 'xiaomi TV 3 s 48 Inch ', 'price' => 2599 ));

Index. php

<? Php header ("content-type: text/html; charset = UTF-8"); require 'common. php '; session_start (); $ sum = 0; $ class = ""; // you can specify if (! Empty ($ _ SESSION ['shopcar ']) {$ data = $ _ SESSION ['shopcar']; $ sum = array_sum ($ data [4]); $ class = "on"; // the upper-right dot if (empty ($ data [0]) {$ class = "" ;}}?> <! DOCTYPE html> 

Action. php

<? Phpif (! Empty ($ _ GET ['id']) {require 'common. php '; session_start (); $ id = $ _ GET ['id']; // print the product information of the selected ID through foreach ($ arrPro as $ key => $ 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 (empty ($ _ SESSION ['shopcar ']) {$ _ 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']; $ arrDataz [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

<? Phpif (! Empty ($ _ GET ['id']) {require 'common. php '; session_start (); $ id = $ _ GET ['id']; // print the product information of the selected ID through foreach ($ arrPro as $ key => $ 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 (empty ($ _ SESSION ['shopcar ']) {$ _ 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']; $ arrDataz [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

<? Phpsession_start (); if (! Empty ($ _ GET ['id']) {$ arrData = $ _ SESSION ['shopcar ']; // determine the corresponding item 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 ');}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.