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

Jul 13, 2016 am 10:44 AM
cookie php several kinds use Example accomplish method have use shopping cart

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
 代码如下 复制代码

<?php
/*
作者:简单小屋
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 “输入的数量不正确.<br>”;
 }
}
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]  <hr />n”; 
        } 

}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “<html xmlns=”<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>无标题文档</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 />
  商品名:<?php echo $row[title]; ?><br />
  价格:<?php echo $row[price]; ?><br />
  数量:
  <input name=”number” type=”text” value=”1″ size=”5″ />
  <input name=”ok” type=”submit” value=”购买”/>
</form>
<hr />
<?php
}
?>
</body>
</html>

Copy code


<?php

/*

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.<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] <hr />n”;          }  } ?> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “<html xmlns=”<head> <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /> <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=”Purchase”/> </form> <hr /> <?php<🎜> }<🎜> ?> </body> </html> 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...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles