首頁 > 後端開發 > php教程 > php购物车问题

php购物车问题

WBOY
發布: 2016-06-23 14:04:01
原創
1432 人瀏覽過

<?php  include ('book_sc_fns.php');  // The shopping cart needs sessions, so start one  session_start();  @$new = $_GET['new'];  if($new) {    //new item selected    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 we have just added an item to the cart, continue shopping in that category  if($new)   {    $details =  get_book_details($new);    if($details['catid']) {      $target = "show_cat.php?catid=".$details['catid'];    }  }  display_button($target, "continue-shopping", "Continue Shopping");  // use this if SSL is set up  // $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");  // if no SSL use below code  display_button("checkout.php", "go-to-checkout", "Go To Checkout");  do_html_footer();?>
登入後複製


代码中的$_SESSION['cart'][$new]是什么意思,为什么要这样子做?


回复讨论(解决方案)

你打印你的$_SESSION看看就知道的。

$_SESSION 中储存了一个叫cart 的数组对象,取出来就是$_SESSION['cart']  $new是这个数组的键值,取法就是$_SESSION['cart'][$new]了

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板