Jump to content

ShoppingCart Help


blackwolf23

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]

Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.