-1

I've been following the Book-O-Rama example for the shopping cart functionality but I can't seem to get it to work. I have the following error when I go into the show_cart.php page.

Notice: Undefined index: cart in C:\wamp\www\site\show_cart.php on line 32
Call Stack
#   Time    Memory  Function    Location
1   0.0005  138616  {main}( )   ...\show_cart.php:0

and

Fatal error: Call to undefined function display_button() in C:\wamp\www\site\show_cart.php on line 45
Call Stack
#   Time    Memory  Function    Location
1   0.0005  138616  {main}( )   ...\show_cart.php:0

The code that I have is:

<?php 
 require_once('require_fns.php');
 session_start();
 @$new = $_GET['new'];
 if($new) {
    //new item selected
    if(!isset($_SESSION['cart'])) {
      $_SESSION['cart'] = array();
      $_SESSION['items'] = 0;
      $_SESSION['price'] ='0.00';
    }
    if(isset($_SESSION['cart'][$new])) {
      $_SESSION['cart'][$new]++;
    } else {
      $_SESSION['cart'][$new] = 1;
    }
    $_SESSION['price'] = calculate_price($_SESSION['cart']);
    $_SESSION['items'] = calculate_items($_SESSION['cart']);
  }
  if(isset($_POST['save'])) {
    foreach ($_SESSION['cart'] as $subid => $qty) {
      if($_POST[$subid] == '0') {
        unset($_SESSION['cart'][$subid]);
      } else {
        $_SESSION['cart'][$subid] = $_POST[$subid];
      }
    }
    $_SESSION['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_subscription_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_user_menu();

do_html_footer();
?>

It's probably me just me missing out on a spelling mistake or something but I don't know how to fix it

JonnySmith
  • 23
  • 6
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Epodax Jun 15 '16 at 09:52

1 Answers1

0

Update line no 32 in show_cart.php

if(isset($_SESSION['cart']) && (array_count_values($_SESSION['cart']))) {
Jignesh Patel
  • 1,028
  • 6
  • 10