php实现购物车功能(上)

2019-12-13 13:11:54于海丽

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 border="0" width="100%" 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" name="name" value="" maxlength="40" size="40"/></td> 
   </tr> 
   <tr> 
   <td>Address</td> 
   <td><input type="text" name="address" value="" maxlength="40" size="40"/></td> 
   </tr> 
   <tr> 
   <td>City/Suburb</td> 
   <td><input type="text" name="city" value="" maxlength="20" size="40"/></td> 
   </tr> 
   <tr> 
   <td>State/Province</td> 
   <td><input type="text" name="state" value="" maxlength="20" size="40"/></td> 
   </tr> 
   <tr> 
   <td>Postal Code or Zip Code</td> 
   <td><input type="text" name="zip" value="" maxlength="10" size="40"/></td> 
   </tr> 
   <tr> 
   <td>Country</td> 
   <td><input type="text" name="country" 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" name="ship_name" maxlength=""/></td> 
   </tr> 
   <tr> 
   <td>Address</td> 
   <td><input type="text" name="ship_address" value="" maxlength="40" size="40"/></td> 
   </tr> 
   <tr> 
   <td>City/Suburb</td> 
   <td><input type="text" name="ship_city" value="" maxlength="20" size="40"/></td> 
   </tr> 
   <tr> 
   <td>State/Province</td> 
   <td><input type="text" name="ship_state" value="" maxlength="20" size="40"/></td> 
   </tr> 
   <tr> 
   <td>Postal Code or Zip Code</td> 
   <td><input type="text" name="ship_zip" value="" maxlength="10" size="40"/></td> 
   </tr> 
   <tr> 
   <td>Country</td> 
   <td><input type="text" name="ship_country" value="" maxlength="20" size="40"/></td> 
   </tr> 
   <tr> 
   <td colspan="2" align="center"> 
    <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 
 } 
								 
			 
相关文章 大家在看