Jump to content


Photo

ShoppingCart Help


  • Please log in to reply
1 reply to this topic

#1 blackwolf23

blackwolf23
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 31 August 2006 - 03:27 AM

Hello everyone,

I have been learning PHP for about 4 weeks now and I am having some difficulty getting my shopping cart to work.

Basically,I allow a user to query the db for a specific book and once that book is found,the details of the book are displayed to user user which are bookname,ISBN,author and price.The page also displays a quantity field to allow the user to specify the number of items they wish to purchase as well as an Add to cart button that executes the cart.php script.

This is where I am having trouble as I cannot get my cart to display the items in the user's shopping cart as it displays a  message saying the cart contains an array when the line
  $_SESSION['cart'] = array();

I do apologise in advance if I have made some simple mistakes but I'm finding some difficulty understanding how to develop a shopping cart and I have tried learning from the examples on the web but I have had no luck.

<?php



 session_start();
 $bookname = $_POST['bookname'];
 $isbn = $_POST['isbn'];
 $author = $_POST['author'];
 $price = $_POST['price'];


if($book)
  {
    //new item selected
    if(!isset($_SESSION['cart']))
    {
      $_SESSION['cart'] = array();
      $_SESSION['items'] = $quantity;

    }

    if(isset($_SESSION['cart'][$book]))
      $_SESSION['cart'][$book]++;
    else
      $_SESSION['cart'][$book] = 1;


  }


if($_SESSION['cart']&&array_count_values($_SESSION['cart']))
    //display_cart($_SESSION['cart']);
    echo 'Your cart contains: '.$_SESSION['cart']. ' <br/>';

  else
  {
    echo '<p>There are no items in your cart</p>';
    echo '<hr />';
  }

?>



#2 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 31 August 2006 - 06:33 AM

Change $_SESSION['cart'] to $_SESSION['cart'][$book].  That should do it.

Change
if($_SESSION['cart']&&array_count_values($_SESSION['cart']))
    //display_cart($_SESSION['cart']);
    echo 'Your cart contains: '.$_SESSION['cart']. ' <br/>';
to

if($_SESSION['cart'])
    //display_cart($_SESSION['cart']);
    echo 'Your cart contains: ' . array_count_values($_SESSION['cart']) .  ' <br/>';

JayBachatero
SMF Developer && Converter Specialist




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users