blackwolf23 Posted August 31, 2006 Share Posted August 31, 2006 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] Quote Link to comment Share on other sites More sharing options...
JayBachatero Posted August 31, 2006 Share Posted August 31, 2006 [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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.