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()
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=https://www.jb51.net/article/\"images/". $book['isbn'] .".jpg\" style=https://www.jb51.net/article/\"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
<?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()
function display_cart($cart,$change = true,$images = 1) //显示购物车 { echo "<table border=https://www.jb51.net/article/\"0\" width=https://www.jb51.net/article/\"100%\" cellspacing=https://www.jb51.net/article/\"0\"> <form action=https://www.jb51.net/article/\"show_cart.php\" method=https://www.jb51.net/article/\"post\"> <tr> <th colspan=https://www.jb51.net/article/\"". (1 + $images) ."https://www.jb51.net/article/\" bgcolor=https://www.jb51.net/article/\" #cccccc\">Item</th> <th bgcolor=https://www.jb51.net/article/\"#cccccc\">Price</th> <th bgcolor=https://www.jb51.net/article/\"#cccccc\">Quantity</th> <th bgcolor=https://www.jb51.net/article/\"#cccccc\">Total</th> </tr>"; //输出购物车中每一项 foreach($cart as $isbn => $qty) { $book = get_book_details($isbn); echo "<tr>"; if($images == true) { echo "<td align=https://www.jb51.net/article/\"left\">"; if(file_exists("images/". $isbn .".jpg")) { $size = getimagesize("images/". $isbn .".jpg"); if(($size[0] > 0) && ($size[1] > 1)) //图片长宽 { echo "<img src=https://www.jb51.net/article/\"images/". $isbn .".jpg\" style=https://www.jb51.net/article/\"border: 1px solid black\" width=https://www.jb51.net/article/\"". ($size[0] / 3) ."https://www.jb51.net/article/\" height=https://www.jb51.net/article/\"". ($size[1] / 3) ."https://www.jb51.net/article/\"/>"; } } else echo " "; echo "</td>"; } echo "<td align=https://www.jb51.net/article/\"left\"> <a href=https://www.jb51.net/article/\"show_book.php?isbn=". $isbn ."https://www.jb51.net/article/\">". $book['title'] ."</a> by". $book['author'] ."</td> <td align=https://www.jb51.net/article/\"center\">\$". number_format($book['price'],2) ."</td><td align=https://www.jb51.net/article/\"center\">"; //如果允许更改数量 if ($change == true) { echo "<input type=https://www.jb51.net/article/\"text\" name=https://www.jb51.net/article/\"".$isbn."https://www.jb51.net/article/\" value=https://www.jb51.net/article/\"".$qty."https://www.jb51.net/article/\" size=https://www.jb51.net/article/\"3\">"; } else { echo $qty; } echo "</td><td align=https://www.jb51.net/article/\"center\">\$".number_format($book['price']*$qty,2)."</td></tr>\n"; } //总数 echo "<tr> <th colspan=https://www.jb51.net/article/\"". (2 + $images) ."https://www.jb51.net/article/\" bgcolor = \"#cccccc\"> </th> <th align = \"center\" bgcolor=https://www.jb51.net/article/\"#cccccc\">". $_SESSION['items'] ."</th> <th align = \"center\" bgcolor=https://www.jb51.net/article/\"#cccccc\">\$". number_format($_SESSION['total_price'],2) ."</th></tr>"; //保存按钮 if($change == true) { echo "<tr> <td colspan = \"". (2 + $images) ."https://www.jb51.net/article/\"> </td> <td align = \"center \"> <input type=https://www.jb51.net/article/\"hidden\" name=https://www.jb51.net/article/\"save\"value=https://www.jb51.net/article/\"true\" /> <input type = \"image\" src = \"images/save-changes.gif\" border = \" 0 \" alt = \" Save Changes \" /> </td> <td> </td> </tr>"; } echo "</form></table>"; }
5.3 book_fns.php文件中的函数calculate_price()