<p style="text-align: left">本文分两篇为大家介绍
PHP实现购物车功能
,具有一定的参考价值,相信大家一定喜欢。<p style="text-align: left">
1、需求分析
<p style="text-align: left">我们需要找到一种将数据库连接到用户的浏览器的方法。用户能够按目录浏览商品。用户应该能够从商品目录中选取商品以便此后的购买。我们也要能够记录他们选中的物品。当用户完成购买,要合计他们的订单,获取运送商品细节,并处理付款。创建一个管理界面,以便管理员在上面添加、编辑图书和目录。
<p style="text-align: left">
2、解决方案
<p style="text-align: left">2.1 用户视图
<p style="text-align: center">
管理员视图
2.3 Book-O-Rama中的文件列表
USE book_sc; #使用book_sc数据库
CREATE TABLE customers #创建用户表
(
customerid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,name CHAR(60) NOT NULL,address CHAR(80) NOT NULL,city CHAR(30) NOT NULL,state CHAR(10),zip CHAR(10),country CHAR(20) NOT NULL
);
CREATE TABLE orders #创建订单表
(
orderid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,customerid INT UNSIGNED NOT NULL,amount FLOAT(6,2),date DATE NOT NULL,order_status CHAR(10),ship_name CHAR(60) NOT NULL,ship_address CHAR(80) NOT NULL,ship_city CHAR(30) NOT NULL,ship_state CHAR(20),ship_zip CHAR(10),ship_country CHAR(20) NOT NULL
);
CREATE TABLE books #创建图书表
(
isbn CHAR(13) NOT NULL PRIMARY KEY,author CHAR(80),title CHAR(100),catid INT UNSIGNED,price FLOAT(4,2) NOT NULL,description VARCHAR(255)
);
CREATE TABLE categories #创建目录表
(
catid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,catname CHAR(60) NOT NULL
);
CREATE TABLE order_items #订单物品表
(
orderid INT UNSIGNED NOT NULL,isbn CHAR(13) NOT NULL,item_price FLOAT(4,quantity TINYINT UNSIGNED NOT NULL,PRIMARY KEY(orderid,isbn)
);
CREATE TABLE admin #管理员表
(
username char(16) NOT NULL PRIMARY KEY,password CHAR(40) NOT NULL
);
GRANT SELECT,INSERT,UPDATE,DELETE
on book_sc.*
to book_sc@localhost IDENTIFIED by 'password';
3.2 数据库测试数据文档
INSERT INTO books VALUES ('0672329166','Luke Welling and Laura Thomson','PHP and MysqL Web Development',1,49.99,'PHP & MysqL Web Development teaches the reader to develop dynamic,secure e-commerce web sites. You will learn to integrate and implement these technologies by following real-world examples and working sample projects.');
INSERT INTO books VALUES ('067232976X','Julie Meloni','Sams Teach Yourself PHP,MysqL and Apache All-in-One',34.99,'Using a straightforward,step-by-step approach,each lesson in this book builds on the prevIoUs ones,enabling you to learn the essentials of PHP scripting,MysqL databases,and the Apache web server from the ground up.');
INSERT INTO books VALUES ('0672319241','Sterling Hughes and Andrei Zmievski','PHP Developer\'s Cookbook',39.99,'Provides a complete,solutions-oriented guide to the challenges most often faced by PHP developers\r\nWritten specifically for experienced Web developers,the book offers real-world solutions to real-world needs\r\n');
INSERT INTO categories VALUES (1,'Internet');
INSERT INTO categories VALUES (2,'Self-help');
INSERT INTO categories VALUES (5,'Fiction');
INSERT INTO categories VALUES (4,'Gardening');
INSERT INTO admin VALUES ('admin',sha1('admin'));
4、实现在线目录
主页-目录
由以下代码实现: 4.1 index.PHP/**
- @author switch
- @copyright 2015
- 网站首页,显示系统中的图书目录
*/
//require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。
require_once('book_sc_fns.PHP');
session_start(); //开始会话
do_html_header('Welcome to Book-O-Rama'); //页头
echo "
Please choose a category:
";$cat_array = get_categories(); //从数据库获取目录
display_categories($cat_array); //显示目录链接
if(isset($_SESSION['admin_user'])) //如果是管理员,显示管理员操作
display_button("admin.PHP","admin-menu","Admin Menu");
do_html_footer(); //页尾
?>
4.2 book_fns.PHP文件中的函数get_categories()
4.3 output_fns.PHP文件中的函数display_categories()
No categories currently available
"; return; } echo "- ";
foreach($cat_array as $row)
{
$url = "show_cat.PHP?catid=". $row['catid'];
$title = $row['catname'];
echo "
- "; do_html_URL($url,$title); echo " "; } echo "
"; }
4.4 db_fns.PHP文件中的函数db_result_to_array()
$res_array[$count] = $row;
return $res_array;
}
Internet目录下的所有图书
代码实现:
PHP
- @author switch
- @copyright 2015
- 显示特定目录包含的所有图书
*/
//require_once语句和require语句完全相同,如果是则不会再次包含。
require_once('book_sc_fns.php');
session_start();
@$catid = $_GET['catid'];
$name = get_category_name($catid);
do_html_header($name);
$book_array = get_books($catid);
display_books($book_array);
//如果是管理员,显示管理界面按钮
if(isset($_SESSION['admin_user']))
{
display_button("index.php","continue","Continue Shopping");
display_button("admin.php","Admin Menu");
display_button("edit_category_form.php?catid=". $catid,"edit-category","Edit Category");
}
else //否则显示主界面按钮
{
display_button("index.php","continue-shopping","Continue Shopping");
}
do_html_footer();
?>
4.6 book_fns.PHP文件中的函数get_category_name()
$num_cats = @$result ->num_rows;
if($num_cats == 0) //查询失败,原因为无目录
return false;
$row = $result ->fetch_object();
return $row ->catname;
}
4.8 book_fns.PHP文件中的函数get_books()
$query = "select * from books where catid = '". $catid ."'";
$result = @$conn ->query($query);
if(!$result) //查询失败,原因为查询出错
return false;
$num_books = @$result ->num_rows;
if($num_books == 0) //查询失败,原因为无图书
return false;
$result = db_result_to_array($result);
return $result;
}
4.9 output_fns文件中的函数display_books()
No books currently available in this category
"; else //有图书,建表 { echo "已填写好信息的订单
获取客户信用卡信息
代码实现: 5.7 purchase.PHP
/**
- @author switch
- @copyright 2015
- 从用户获取付款细节
*/
//require_once语句和require语句完全相同,如果是则不会再次包含。
require_once('book_sc_fns.PHP');
session_start();
do_html_header("Checkout");
//创建变量
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$country = $_POST['country'];
//如果订单细节填满
if(($_SESSION['cart']) && ($name) && ($address) && ($city) && ($zip) && ($country))
{
if(insert_order($_POST) != false)
{
display_cart($_SESSION['cart'],0);
display_shipping(calculate_shipping_cost());
display_card_form($name);
display_button("show_cart.PHP","Continue Shopping");
}
else
{
echo "
Could not store data,please try again.
";
display_button('checkout.PHP','back','Back');
}
}
else
{
echo "
You did not fill in all the fields,please try again.
";
display_button('checkout.PHP','Back');
}
do_html_footer();
?>
5.8 order_fns.PHP文件中的函数insert_order()
<div class="jb51code">
<pre class="brush:php;">
function insert_order($order_details) //提取订单细节作为变量
{
extract($order_details);
//设置邮寄地址为当前地址
if((!$ship_name) && (!$ship_address) && (!$ship_city) && (!$ship_state) && (!$ship_zip) &&(!$ship_country))
{
$ship_name = $name;
$ship_address = $address;
$ship_city = $city;
$ship_state = $state;
$ship_zip = $zip;
$ship_country = $country;
}
//连接数据库
$conn = db_connect();
//事务开始,必须关闭自动提交
$conn ->autocommit(false);
$query = "select customrid from customers where
name ='". $name ."' and address = '". $address ."'
and city = '". $city ."' and state = '". $state ."'
and zip = '". $zip ."' and country = '". $country ."'";
$result = $conn ->query($query);
if(@$result ->num_rows > 0)
{
$customer = $result ->fetch_object();
$customerid = $customer ->customerid;
}
else
{
$query = "insert into customers values
('','". $name ."','". $address ."','". $city ."','". $state ."','". $zip ."','". $country ."')";
$result = $conn ->query($query);
if(!$result)
return false;
}
$customerid = $conn ->insert_id; //返回上次查询中自增量的ID
$date = date("Y-m-d");
$query ="insert into orders values
('','". $customerid ."','". $_SESSION['total_price'] ."','". $date ."','PARTIAL','". $ship_name ."','". $ship_address ."','". $ship_city ."','". $ship_state ."','". $ship_zip ."','". $ship_country ."')";
$result = $conn ->query($query);
if(!$result)
return false;
$query = "select orderid from orders where
customerid ='". $customerid ."' and
amount > (". $_SESSION['total_price'] ."-.001) and
amount < (". $_SESSION['total_price'] ."+.001) and
date ='". $date ."' and
order_status = 'PARTIAL' and
ship_name ='". $ship_name ."' and
ship_address ='". $ship_address ."' and
ship_city ='". $ship_city ."' and
ship_state ='". $ship_state ."' and
ship_zip ='". $ship_zip ."' and
ship_country ='". $ship_country ."'";
$result = $conn ->query($query);
if($result ->num_rows > 0)
{
$order = $result ->fetch_object();
$orderid = $order ->orderid;
}
else
return false;
foreach($_SESSION['cart'] as $isbn => $quantity)
{
$detail = get_book_details($isbn);
$query = "delete from order_items where
orderid = '". $orderid ."' and isbn = '". $isbn ."'";
$result = $conn ->query($query);
$query = "insert into order_items values
('". $orderid ."','". $isbn ."',". $detail['price'] .",$quantity)";
$result = $conn ->query($query);
if(!$result)
return false;
}
//事务关闭,开启自动提交
$conn ->commit();
$conn ->autocommit(true);
return $orderid;
}