Jump to content

Archived

This topic is now archived and is closed to further replies.

blackwolf23

ShoppingCart Help

Recommended Posts

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 [code]  $_SESSION['cart'] = array(); [/code]

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.

[code]
<?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 />';
  }

?>
[/code]

Share this post


Link to post
Share on other sites
[s]Change [tt]$_SESSION['cart'][/tt] to [tt]$_SESSION['cart'][$book][/tt].  That should do it.[/s]

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

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.