Home > Backend Development > PHP Tutorial > Using cookies to implement shopping cart examples in PHP_PHP tutorial

Using cookies to implement shopping cart examples in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:44:07
Original
1491 people have browsed it

The most commonly used methods for implementing shopping carts include cookies, sessions and saving records to the database. Below I will introduce the simplest method, which is to use cookies as the product record storage library for shopping carts.

PHP shopping cart, there are many online stores on the Internet, how do they implement shopping carts? Most websites use cookies to achieve this. I also wrote a simple example for your reference

Easy implementation of shopping cart using cookies

Database:

The code is as follows Copy code
 代码如下 复制代码

– phpMyAdmin SQL Dump
– version 2.11.9.2

– 主机: 127.0.0.1:3306
– 生成日期: 2009 年 12 月 06 日 02:05
– 服务器版本: 5.1.28
– PHP 版本: 5.2.6

SET SQL_MODE=”NO_AUTO_VALUE_ON_ZERO”;


– 数据库: `shopper`

– ——————————————————–


– 表的结构 `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 ;


– 导出表中的数据 `shop`

INSERT INTO `shop` (`id`, `price`, `title`) VALUES
(1, 100, ‘玉米’),
(2, 200, ‘大豆’),
(3, 500, ‘西瓜’),
(4, 900, ‘冬瓜’),
(5, 800, ‘大米’);

– phpMyAdmin SQL Dump
– version 2.11.9.2

– Host: 127.0.0.1:3306
– Generated date: December 06, 2009 02:05
– 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 data in the table `shop`
– INSERT INTO `shop` (`id`, `price`, `title`) VALUES
(1, 100, ‘Corn’),
(2, 200, ‘soy’),
(3, 500, ‘watermelon’),
(4, 900, ‘Winter Melon’),
(5, 800, ‘rice’);

PHP code file:

QQ group 1: 32311900 (full) QQ group 2: 50900416
The code is as follows
 代码如下 复制代码

/*
作者:简单小屋
QQ群1:32311900(满)
QQ群2:50900416
QQ2:39407******(满)简单小屋
QQ2:8726**** 海角
*/
$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 “输入的数量不正确.
”;
 }
}
if(isset($_COOKIE['cookie_arr'])){ 
     foreach($_COOKIE['cookie_arr']   as   $name   =>   $value)   {
                   $value2=explode(“|”,$value); 
                  echo   “ID({$name}) — $value2[0] — 数量:$value2[1]   –  单价:$value2[2]  –  总价格:$value2[3] 


n”; 
        } 

}
?>


无标题文档



while($row=mysql_fetch_array($sql2)){
?>

  ” />
  ” />
  ” />
  id:

  商品名:

  价格:

  数量:
 
 



}
?>

Copy code


/*

Author: Simple House
QQ2:39407******(full)Simple cabin

QQ2:8726**** Cape

*/

$conn=mysql_connect("localhost","root",""); 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 entered quantity 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)){<🎜> ?>
” /> ” /> ” /> id:
Product name:
Price:
Quantity:

}<🎜> ?> Disadvantage Analysis Cookies are easy to lose as a shopping cart. If the user clears the browser cache, the cookie value may be lost. Therefore, under normal circumstances, cookies + database are used for examples. http://www.bkjia.com/PHPjc/633124.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633124.htmlTechArticleThe most commonly used methods of shopping cart implementation are cookie, session and saving records to the database. Below I Let’s introduce the simplest way to use cookies as product record storage in the shopping cart...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template