增加商品到购物车
代码如下 | 复制代码 |
<?php // // 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 // Loop through all the products and pull up the product
foreach ($master_products_list as $prod_id => $product) { // Register our session
if ($_POST[ordered]) { // If they have chosen the product array_push($_SESSION[cart][products], array(trim($_POST[id]), $_POST[quantity])); <html> <a href="cart.php">返回</a> 商品列表页面. <form action="<?php echo $PHP_SELF; ?>" method="post"> <input type="submit" value="添加至购物栏"> |
查看购物车的商品
代码如下 | 复制代码 |
<?php // // cart.php: www.111cn.net // session_start(); require 'lib.inc.php'; // 如果购物篮没有初始化,则初始化购物篮 // From site_lib.inc, Loads the $master_products_list array <html> <body> <h1>欢迎进入网上商店</h1> <?php $total += $quantity * <h2>商店待出售的商品</h2> ?> |
修改购物车的数量
代码如下 | 复制代码 |
<?php // // 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 // Save the old number of products for display if ($_POST[quantity]) { // Update the number of items <html> |
功能页面,用户把购物车里面的内容保存到txt数据库
代码如下 | 复制代码 |
<?php //物品数组 $master_products_list = array(); //载入物品数据函数 $fp = @fopen($filename, "r") //读取文件内容 @fclose($fp) //关闭文件 |
很简单,我们只用了4个文件就实现用php 做好购物车功能哦,好了这只是一款简单的php购物车代码更复杂的需要考虑更多更好。