The most common methods for implementing the shopping cart are cookie, session, and storing records in the database. next I will introduce the simplest method to use cookie as the product record repository of the shopping cart.
The most common methods for implementing the shopping cart are cookie, session, and storing records in the database. next I will introduce the simplest method to use cookie as the product record store of the shopping cart.
PHP shopping cart. There are many online stores on the internet. how do they implement shopping cart? Most websites use cookies, and even write a simple example for your reference.
The database code is as follows:
- -PhpMyAdmin SQL Dump
- -Version 2.11.9.2
- -
- -Host: 127.0.0.1: 3306
- -Generation date: December 06, 2009
- -Server version: 5.1.28
- -PHP version: 5.2.6
- SET SQL _MODE = "NO_AUTO_VALUE_ON_ZERO ";
- -
- -Database: 'shopper'
- -
- --------------------
- -
- -Table structure 'shop'
- -
- Create table if not exists 'shop '(
- 'Id' int (11) not null AUTO_INCREMENT,
- 'Price' int (11) not null,
- 'Title' varchar (110) not null,
- Primary key ('id ')
- ) ENGINE = MyISAM default charset = utf8 AUTO_INCREMENT = 6;
- -
- -Export the data 'shop' in the table'
- -
- Insert into 'shop '('id', 'price', 'title') VALUES
- (1,100, 'corn '),
- (2,200, 'soybean '),
- (3,500, 'watermelon '),
- (4,900, 'melon '),
- (5,800, 'Rice ');
PHP code file:
-
- /*
- Author: simple hut
- QQ Group: 1: 32311900 (full)
- QQ Group 2: 50900416
- QQ2: 39407 * (full) simple hut
- QQ2: 8726 * cape
- */
- $ Conn = mysql_connect ("localhost", "root ","");
- Mysql_select_db ("shopper", $ conn );
- Mysql_query ("set names utf8 ″);
- $ SQL = "SELECT * FROM 'shop 'WHERE 1";
- $ Sql2 = mysql_query ($ SQL );
- If ($ _ POST [OK]) {
- $ _ POST [number] = (int) $ _ POST [number];
- If ($ _ POST [number]> 0 ){
- $ Idid = $ _ POST [id];
- Setcookie ("cookie_arr [$ idid]", $ _ POST [title]. "| ". $ _ POST [number]. "| ". $ _ POST [price]. "| ". $ _ POST [number] * $ _ POST [price], time () + 36000 );
- Header ("location: shop. php ");
- } Else {
- Echo "The number entered is incorrect.
";
- }
- }
- If (isset ($ _ COOKIE ['cookie _ arr']) {
- Foreach ($ _ COOKIE ['cookie _ arr'] as $ name => $ value ){
- $ Value2 = explode ("|", $ value );
- Echo "ID ({$ name})-$ value2 [0]-quantity: $ value2 [1]-unit price: $ value2 [2]-Total Price: $ value2 [3] N ";
- }
- }
- ?>
-
-
- Untitled Document
-
-
-
- While ($ row = mysql_fetch_array ($ sql2 )){
- ?>
-
-
-
- }
- ?>
-
-
Disadvantage analysis:Cookie is easily lost as a shopping cart. if the user clears the browser cache, the cookie value may be lost. therefore, generally, the cookie + database is used as an instance.