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 ">
★
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!