Home >Backend Development >PHP Tutorial >PHP shopping cart implementation method_PHP tutorial

PHP shopping cart implementation method_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:59:311060browse

PHP shopping cart implementation method

This article mainly introduces the php shopping cart implementation method, realizing the shopping cart function through 4 files, and using txt files to save the shopping cart content , simple and practical, friends in need can refer to it

The example in this article describes the implementation method of php shopping cart. Share it with everyone for your reference. The specific analysis is as follows:

Here we provide you with a simple php shopping cart code, from adding shopping products to making purchases. In mall development, this function is indispensable. We do not need a database and use txt text files to operate user shopping. content.

Add items to the shopping cart with the following code:

The code is as follows:

//
// add_item.php:
// Add an item to the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
session_register('cart');
}

require 'lib.inc.php'; // LoadProducts()

LoadProducts(); // Load products in $master_products_list

// Make $curr_product global
$curr_product = array();

// Loop through all the products and pull up the product
// that we are interested in

foreach ($master_products_list as $prod_id => $product) {
if (trim($prod_id) == trim($_GET[id])) {
$curr_product = $product;
}
}

// Register our session
//session_register('cart');
//if(session_is_registered('cart')) echo "already registered";

if ($_POST[ordered]) { // If they have chosen the product

array_push($_SESSION[cart][products], array(trim($_POST[id]), $_POST[quantity]));
$_SESSION[cart][num_items] = $_POST[quantity];
}
?>


<br> <?php if ($_POST[ordered]) { ?> <br> Already added <?php echo $curr_product[name]; ?> to your shopping cart <br> <?php } else { ?> <br> Add <?php echo $curr_product[name]; ?> to your basket <br> <?php } ?> <br>




Added to shopping cart successfully



Return product list page.

Add to your shopping cart




Product name:


Product description:


Product unit price: RMB


Product quantity:






View the items in the shopping cart, the code is as follows:

The code is as follows:

//
// cart.php:
//
session_start();

require 'lib.inc.php';
//Determine whether the shopping basket session variable cart is registered. If not, register the cart variable
if (session_is_registered('cart')) {
session_register('cart');
}

// If the shopping basket has not been initialized, initialize the shopping basket
if (!isset($_SESSION[cart][num_items])) {
$_SESSION[cart] = array("num_items" => 0,
"products" => array());
}
// From site_lib.inc, Loads the $master_products_list array
LoadProducts(); //Load item list
?>


Shopping basket program demonstrating session tracking




Welcome to the online store



if ($_SESSION[cart][num_items]) { // If there is something to show
?>

Items currently in the shopping basket













// Loop through the products
foreach ($_SESSION[cart][products] as $i => $product) {
$product_id = $product[0];
$quantity = $product[1];

$total = $quantity *
(double)$master_products_list[$product_id][price];
?>







}
?>






Product name

Product Description

Unit price

Quantity











value="">




Total:

RMB:





}
?>

Items for sale in the store





We offer the following items for sale:










// Show all of the products
foreach ($master_products_list as $product_id => $item) {
?>






}

?>

Product name

Product Description

Unit price







$


Add to basket

 

修改购物车的数量,代码如下:

代码如下:

//
// change_quant.php:
// Change the quantity of an item in the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
session_register('cart');
}

// Typecast to int, making sure we access the
// right element below
$i = (int)$_POST[id];

// Save the old number of products for display
// and arithmetic
$old_num = $_SESSION[cart][products][$i][1];

if ($_POST[quantity]) {
$_SESSION[cart][products][$i][1] = $_POST[quantity]; //change the quantity
} else {
unset($_SESSION[cart][products][$i]); // Send the product into oblivion
}

// Update the number of items
$_SESSION[cart][num_items] = ($old_num >$_POST[quantity]) ?
$_SESSION[cart][num_items] - ($old_num-$_POST[quantity]) :
$_SESSION[cart][num_items] ($_POST[quantity]-$old_num);
?>



<br> 数量修改 <br>


将数量: 更改为


返回 商品列表页面.

 

功能页面,用户把购物车里面的内容保存到txt数据库,代码如下:

代码如下:

//物品数组
$master_products_list = array();


//载入物品数据函数
function LoadProducts() {
global $master_products_list;
$filename = 'products.txt';

$fp = @fopen($filename, "r")
or die("打开 $filename 文件失败");
@flock($fp, 1)
or die("锁定 $filename 文件失败");

//读取文件内容
while ($line = fgets($fp, 1024)) {
list($id, $name, $desc, $price) = explode('|', $line); //读取每行数据,数据以| 格开
$id = trim($id); //去掉首尾特殊符号
$master_products_list[$id] = array("name" => $name, //名称
"desc" => $desc, //说明
"price" => $price); //单价
}

@fclose($fp) //关闭文件
or die("关闭 $filename 文件失败");
}
?>
很简单,我们只用了4个文件就实现用php 做好购物车功能,好了这只是一款简单的php购物车代码更复杂的需要考虑更多更好.

 

希望本文所述对大家的php程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/975888.htmlTechArticlephp购物车实现方法 这篇文章主要介绍了php购物车实现方法,通过4个文件实现购物车的功能,且使用txt文件保存购物车内容,简单实用,需要的朋...
Statement:
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