php实现购物车功能(上)(4)

function calculate_price($cart) //计算购物车中物品总价 { $price = 0.0; if(is_array($cart)) { $conn = db_connect(); foreach($cart as $isbn => $qty) { $query = "select price from books where isbn ='". $isbn ."'"; $result = $conn ->query($query); if($result) { $item = $result ->fetch_object(); $item_price = $item ->price; $price += $item_price * $qty; } } } return $price; }

5.4 book_fns.php文件中的函数calculate_items()

function calculate_items($cart) //计算购物车中的物品总数 { $items = 0; if(is_array($cart)) { foreach($cart as $isbn => $qty) $items += $qty; } return $items; }


获取顾客的详细信息

由以下代码实现:
5.5 checkout.php

<?php /** * @author switch * @copyright 2015 * 向用户显示所有的订单细节。获取商品运送细节 */ //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 require_once('book_sc_fns.php'); session_start(); do_html_header("Checkout"); if((@$_SESSION['cart']) && (array_count_values($_SESSION['cart']))) { display_cart($_SESSION['cart'],false,0); display_checkout_form(); } else { echo "<p>Thers are no items in your cart</p>"; } display_button("show_cart.php","continue-shopping","Continue Shopping"); do_html_footer(); ?>

5.6 output_fns.php文件中的display_checkout_form()

function display_checkout_form() //输出付款台界面 { ?> <br /> <table cellspacng="0"> <form action="purchase.php" method="post"> <tr> <!--客户信息--> <th colspan="2" bgcolor="#cccccc">Your Details</th> </tr> <tr> <td>Name</td> <td><input type="text" value="" maxlength="40" size="40"/></td> </tr> <tr> <td>Address</td> <td><input type="text" value="" maxlength="40" size="40"/></td> </tr> <tr> <td>City/Suburb</td> <td><input type="text" value="" maxlength="20" size="40"/></td> </tr> <tr> <td>State/Province</td> <td><input type="text" value="" maxlength="20" size="40"/></td> </tr> <tr> <td>Postal Code or Zip Code</td> <td><input type="text" value="" maxlength="10" size="40"/></td> </tr> <tr> <td>Country</td> <td><input type="text" value="" maxlength="10" size="40"/></td> </tr> <tr> <!--运单信息--> <th colspan="2" bgcolor="#cccccc">Shipping Address(leave blank if as above)</th> </tr> <tr> <td>Name</td> <td><input type="text" maxlength=""/></td> </tr> <tr> <td>Address</td> <td><input type="text" value="" maxlength="40" size="40"/></td> </tr> <tr> <td>City/Suburb</td> <td><input type="text" value="" maxlength="20" size="40"/></td> </tr> <tr> <td>State/Province</td> <td><input type="text" value="" maxlength="20" size="40"/></td> </tr> <tr> <td>Postal Code or Zip Code</td> <td><input type="text" value="" maxlength="10" size="40"/></td> </tr> <tr> <td>Country</td> <td><input type="text" value="" maxlength="20" size="40"/></td> </tr> <tr> <td colspan="2"> <p> <strong>Please press Purchase to confirm your purchase, or Continue Shopping to add or remove items.</strong> </p> <?php display_form_button("purchase","Purchase There Items"); ?> </td> </tr> </form> </table> <hr /> <?php }


已填写好信息的订单


获取客户信用卡信息

由以下代码实现:
5.7 purchase.php

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/233b49197e1240bf53f3b28b4b1c2f2e.html