本文分两篇为大家介绍php实现购物车功能,具有一定的参考价值,相信大家一定喜欢。
1、需求分析
我们需要找到一种将数据库连接到用户的浏览器的方法。用户能够按目录浏览商品。 用户应该能够从商品目录中选取商品以便此后的购买。我们也要能够记录他们选中的物品。 当用户完成购买,要合计他们的订单,获取运送商品细节,并处理付款。 创建一个管理界面,以便管理员在上面添加、编辑图书和目录。
2、解决方案
2.1 用户视图
2.2 管理员视图
2.3 book-o-rama中的文件列表
3、实现数据库3.1 创建book_sc数据库的sql代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
create database book_sc; #创建book_sc数据库 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,2) not null, 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 数据库测试数据文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
use book_sc; 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' ,1,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' ,1,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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?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 "<p>please choose a category:</p>" ; $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()
1
2
3
4
5
6
7
8
9
10
11
12
13
|
function get_categories() //从数据库中获取目录列表 { $conn = db_connect(); //连接数据库 $query = "select catid,catname from categories" ; $result = @ $conn ->query( $query ); if (! $result ) //查询失败,返回false return false; $num_cats = @ $result ->num_rows; if ( $num_cats == 0) //数据库中无目录,返回false return false; $result = db_result_to_array( $result ); return $result ; } |
4.3 output_fns.php文件中的函数display_categories()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
function display_categories( $cat_array ) //输出目录 { if (! is_array ( $cat_array )) { echo "<p>no categories currently available</p>" ; return ; } echo "<ul>" ; foreach ( $cat_array as $row ) { $url = "show_cat.php?catid=" . $row [ 'catid' ]; $title = $row [ 'catname' ]; echo "<li>" ; do_html_url( $url , $title ); echo "</li>" ; } echo "</ul>" ; echo "<hr/>" ; } |
4.4 db_fns.php文件中的函数db_result_to_array()
1
2
3
4
5
6
7
8
9
|
function db_result_to_array( $result ) //结果到数组 { $res_array = array (); for ( $count = 0; $row = $result ->fetch_assoc(); $count ++) $res_array [ $count ] = $row ; return $res_array ; } |
internet目录下的所有图书
由以下代码实现:
4.5 show_cat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<?php /** * @author switch * @copyright 2015 * 显示特定目录包含的所有图书 */ //require_once语句和require语句完全相同,唯一区别是php会检查该文件是否已经被包含过,如果是则不会再次包含。 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" , "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()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function get_category_name( $catid ) //获取目录名 { $conn = db_connect(); //连接数据库 $query = "select catname from categories where catid = '" . $catid . "'" ; $result = @ $conn ->query( $query ); if (! $result ) //查询失败,原因为查询出错 return false; $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()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
function get_books( $catid ) //从数据库中获取图书 { if ((! $catid ) || ( $catid == '' )) //如果目录id为空 return false; $conn = db_connect(); $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()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
function display_books( $book_array ) //输出图书 { if (! is_array ( $book_array )) echo "<p>no books currently available in this category</p>" ; else //有图书,建表 { echo "<table width = \"100%\" border=\"0\">" ; foreach ( $book_array as $row ) { $url = "show_book.php?isbn=" . $row [ 'isbn' ]; echo "<tr><td>" ; // 如果图片存在 if (@ file_exists ( "images/" . $row [ 'isbn' ] . ".jpg" )) { $title = "<img src=\"images/" . $row [ 'isbn' ] . ".jpg\" style=\"border: 1px solid black\"/>" ; do_html_url( $url , $title ); } else echo " " ; echo "</td><td>" ; $title = $row [ 'title' ] . " by " . $row [ 'author' ]; do_html_url( $url , $title ); echo "</td></tr>" ; } echo "</table>" ; } echo "<hr/>" ; } |
php and mysql web development的详细信息
由以下代码实现:
4.10 show_book.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?php /** * @author switch * @copyright 2015 * 显示特定图书的详细信息 */ //require_once语句和require语句完全相同,唯一区别是php会检查该文件是否已经被包含过,如果是则不会再次包含。 require_once ( 'book_sc_fns.php' ); session_start(); $isbn = $_get [ 'isbn' ]; $book = get_book_details( $isbn ); do_html_header( $book [ 'title' ]); display_book_details( $book ); //设置继续按钮 $target = "index.php" ; if ( $book [ 'catid' ]) $target = "show_cat.php?catid = " . $book [ 'catid' ]; //如果是管理员,显示编辑链接 if (check_admin_user()) { display_button( "edit_book_form.php?isbn=" . $isbn , "edit-item" , "edit item" ); display_button( "admin.php" , "admin-menu" , "admin menu" ); display_button( $target , "continue" , "continue" ); } else { display_button( "show_cart.php?new=" . $isbn , "add-to-cart" , "add" . $book [ 'title' ]. " to my shopping cart" ); display_button( $target , "continue-shopping" , "continue shopping" ); } do_html_footer(); ?> |
4.11 book_fns.php文件中的函数get_book_details()
1
2
3
4
5
6
7
8
9
10
11
12
13
|
function get_book_details( $isbn ) //从数据库中获取一本图书的详细说明 { if ((! $isbn ) || ( $isbn == '' )) //如果图书统一书号为空 return false; $conn = db_connect(); //连接数据库 $query = "select * from books where isbn = '" . $isbn . "'" ; $result = @ $conn ->query( $query ); if (! $result ) //查询失败,原因为查询出错 return false; $result = @ $result ->fetch_assoc(); return $result ; } |
4.12 output_fns.php文件中的函数display_book_details()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
function display_book_details( $book ) //输出图书详细说明 { if ( is_array ( $book )) { echo "<table><tr>" ; // 如果图片存在 if (@ file_exists ( "images/" . $book [ 'isbn' ] . ".jpg" )) { $size = getimagesize ( "images/" . $book [ 'isbn' ] . ".jpg" ); if (( $size [0] > 0) && ( $size [1] > 0)) { echo "<td><img src=\"images/" . $book [ 'isbn' ] . ".jpg\" style=\"border: 1px solid black\"/></td>" ; } } echo "<td><ul>" ; echo "<li><strong>author:</strong>" ; echo $book [ 'author' ]; echo "</li><li><strong>isbn:</strong>" ; echo $book [ 'isbn' ]; echo "</li><li><strong>our price:</strong>" ; echo number_format( $book [ 'price' ],2); echo "</li><li><strong>description:</strong>" ; echo $book [ 'description' ]; echo "</li></ul></td></tr></table>" ; } else { echo "<p>the details of this book cannot be displayed at this time.</p>" ; } echo "<hr/>" ; } |
5、实现购物车
不使用参数的脚本只显示购物车的内容
带有参数new的脚本将添加一个物品到购物车
由以下代码实现:
5.1 show_cart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
<?php /** * @author switch * @copyright 2015 * 显示用户购物车的内容。也用来向购物车添加图书 */ //require_once语句和require语句完全相同,唯一区别是php会检查该文件是否已经被包含过,如果是则不会再次包含。 require_once ( 'book_sc_fns.php' ); session_start(); @ $new = $_get [ 'new' ]; if ( $new ) { if (!isset( $_session [ 'cart' ])) //购物车中无物品 { $_session [ 'cart' ] = array (); $_session [ 'items' ] = 0; $_session [ 'total_price' ] = '0.00' ; } if (isset( $_session [ 'cart' ][ $new ])) { $_session [ 'cart' ][ $new ]++; } else { $_session [ 'cart' ][ $new ] = 1; } $_session [ 'total_price' ] = calculate_price( $_session [ 'cart' ]); $_session [ 'items' ] = calculate_items( $_session [ 'cart' ]); } if (isset( $_post [ 'save' ])) { foreach ( $_session [ 'cart' ] as $isbn => $qty ) { if ( $_post [ $isbn ] == '0' ) unset( $_session [ 'cart' ][ $isbn ]); else $_session [ 'cart' ][ $isbn ] = $_post [ $isbn ]; } $_session [ 'total_price' ] = calculate_price( $_session [ 'cart' ]); $_session [ 'items' ] = calculate_items( $_session [ 'cart' ]); } do_html_header( "your shopping cart" ); if ((@ $_session [ 'cart' ]) && ( array_count_values ( $_session [ 'cart' ]))) { display_cart( $_session [ 'cart' ]); } else { echo "<p>there are no items in your cart</p><hr/>" ; } $target = "index.php" ; //如果只有一种物品添加到购物车,可以继续购物 if ( $new ) { $details = get_book_details( $new ); if ( $details [ 'catid' ]) { $target = "show_cat.php?catid=" . $details [ 'catid' ]; } } display_button( $target , "continue-shopping" , "continue shopping" ); //ssl链接--需要配置,ps:没配置,所以不能使用 // $path = $_server['php_self']; //获取路径 // $server = $_server['server_name']; //获取主机名 // $path = str_replace('show_cart.php','',$path); // display_button("https://". $server . $path ."checkout.php","go-to-checkout","go to checkout"); //非ssl链接 display_button( "checkout.php" , "go-to-checkout" , "go to checkout" ); do_html_footer(); ?> |
5.2 output_fns.php文件中的函数display_cart()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
function display_cart( $cart , $change = true, $images = 1) //显示购物车 { echo "<table border=\"0\" width=\"100%\" cellspacing=\"0\"> <form action=\"show_cart.php\" method=\"post\"> <tr> <th colspan=\ "" . (1 + $images ) ."\" bgcolor=\" #cccccc\">item</th> <th bgcolor=\"#cccccc\">price</th> <th bgcolor=\"#cccccc\">quantity</th> <th bgcolor=\"#cccccc\">total</th> </tr>"; //输出购物车中每一项 foreach ( $cart as $isbn => $qty ) { $book = get_book_details( $isbn ); echo "<tr>" ; if ( $images == true) { echo "<td align=\"left\">" ; if ( file_exists ( "images/" . $isbn . ".jpg" )) { $size = getimagesize ( "images/" . $isbn . ".jpg" ); if (( $size [0] > 0) && ( $size [1] > 1)) //图片长宽 { echo "<img src=\"images/" . $isbn .".jpg\" style=\"border: 1px solid black\" width=\ "" . ( $size [0] / 3) ."\" height=\ "" . ( $size [1] / 3) . "\"/>" ; } } else echo " " ; echo "</td>" ; } echo "<td align=\"left\"> <a href=\ "show_book.php?isbn=" . $isbn . "\">" . $book [ 'title' ] . "</a> by" . $book [ 'author' ] ."</td> <td align=\ "center\">\$" . number_format( $book [ 'price' ],2) . "</td><td align=\"center\">" ; //如果允许更改数量 if ( $change == true) { echo "<input type=\"text\" name=\"" . $isbn . "\" value=\"" . $qty . "\" size=\"3\">" ; } else { echo $qty ; } echo "</td><td align=\"center\">\$" .number_format( $book [ 'price' ]* $qty ,2). "</td></tr>\n" ; } //总数 echo "<tr> <th colspan=\ "" . (2 + $images ) ."\" bgcolor = \"#cccccc\"> </th> <th align = \ "center\" bgcolor=\"#cccccc\">" . $_session [ 'items' ] ."</th> <th align = \ "center\" bgcolor=\"#cccccc\">\$" . number_format( $_session [ 'total_price' ],2) . "</th></tr>" ; //保存按钮 if ( $change == true) { echo "<tr> <td colspan = \ "" . (2 + $images ) ."\"> </td> <td align = \"center \"> <input type=\"hidden\" name=\"save\"value=\"true\" /> <input type = \"image\" src = \"images/save-changes.gif\" border = \" 0 \" php" id="highlighter_338967">
5.4 book_fns.php文件中的函数calculate_items()
获取顾客的详细信息
由以下代码实现:
5.6 output_fns.php文件中的display_checkout_form()
已填写好信息的订单
由以下代码实现:
5.8 order_fns.php文件中的函数insert_order()
5.9 output_fns.php文件中的函数display_shipping()
5.10 output_fns.php文件中的函数display_card_form()
5.11 db_fns.php文件中的函数db_connect()
6、实现付款
已填写好信息的信用卡详细信息
购物成功
由以下代码实现:
以上就是php实现购物车功能的前篇,代码很详细,希望对大家的学习有所帮助,之后还有下篇分享给大家,不要错过。 相关文章
热门资讯 |