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. I wrote a simple example for your reference.
Simple shopping cart with cookies
Database:
The Code is as follows: |
Copy code |
-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:
The Code is as follows: |
Copy code |
<? Php /* 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. <br> "; } } 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] } } ?> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "<Title> untitled document </title> </Head> <Body> <? Php While ($ row = mysql_fetch_array ($ sql2 )){ ?> <Form action = "" method = "post"> <Input name = "id" type = "hidden" value = "<? Php echo $ row [id];?>" /> <Input name = "price" type = "hidden" value = "<? Php echo $ row [price];?>" /> <Input name = "title" type = "hidden" value = "<? Php echo $ row [title];?>" /> Id: <? Php echo $ row [id];?> <Br/> Product Name: <? Php echo $ row [title];?> <Br/> Price: <? Php echo $ row [price];?> <Br/> Quantity: <Input name = "number" type = "text" value = "1" size = "5"/> <Input name = "OK" type = "submit" value = "buy"/> </Form> <Hr/> <? Php } ?> </Body> </Html> |
Defect Analysis
As a shopping cart, cookies are easily lost. If the user clears the browser cache, the cookie value may be lost. Therefore, cookie + database is usually used for instances.