need help with a shopping cart

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dwain_francis
Forum Newbie
Posts: 7
Joined: Wed Mar 26, 2008 11:45 pm

need help with a shopping cart

Post by dwain_francis »

Can someone help me out with this code:

I am sending values via a form to a session array but it is not storing the values, so everytime I click the submit button it is supposed to increment but it is not doing that.

This is the form:

Code: Select all

<form method="post" action="">
   <input name="Title" type="hidden" value="$5 Donation With Shipping" />
  <input name="SerialNum" type="hidden" value="5DollarDonationWS" />
  <input name="Price" type="hidden" value="5.00" />
  <input name="Shipping1" type="hidden" value="1.00" />
  <input name="Shipping2" type="hidden" value="0.50" />
  <input name="cmd" type="hidden" id="cmd" value="1" />
  <input type="submit" name="Submit" value="Add To Cart" />
</form>
and this is the shopping cart script:

Code: Select all

<?php 
session_start();
$_SESSION['cart'] = array(
                                              "title" => $_POST['title'], 
                                              "price" => $_POST['price'], 
                                              "author" => $_POST['author'], 
                                               "qty" => $_POST['cmd']);
  // Check To See If Cart Exists, And Set Count Accordingly  
$new = "";
if(isset($_SESSION['cart']))
{
$_SESSION['cart']++;
}else{
$_SESSION['cart'] = 1;
}
foreach( $_SESSION['cart'] as  $key =>$value)
{
echo '<table border ="0" width ="100%" cellspacing = "0"><tr><th bgcolor="#cccccc">' . $key . '</th><tr><td>' . $value . '</td></tr></table>';
echo $_SESSION['title'];
}
?>

Can someone help me please
Last edited by John Cartwright on Thu Mar 27, 2008 1:18 pm, edited 1 time in total.
Reason: Added [code=php][/code] tags
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: need help with a shopping cart

Post by John Cartwright »

You are trying to store the contents of 1 item in $_SESSION['cart'] -- which will be overwritten everytime. I think you want to be adding items to the cart. Also, you were trying to increment an array, which is incorrect. Use a different variable to store the # of items in the cart, i.e.

Code: Select all

if (!isset($_SESSION['cart'])) {
   $_SESSION['cartnum'] = 0; //number of items in cart
   $_SESSION['cart'] = array(); //initialize the array
}
 
$_SESSION['cartnum']++;
$_SESSION['cart'][] = array( //add item to the cart
   "title" => $_POST['title'],
   "price" => $_POST['price'],
   "author" => $_POST['author'],
   "qty" => $_POST['cmd']
);
Or you can even skip storing the # of items and calculate it by using

Code: Select all

$num = count($_SESSION['cart']);
dwain_francis
Forum Newbie
Posts: 7
Joined: Wed Mar 26, 2008 11:45 pm

Re: need help with a shopping cart

Post by dwain_francis »

I get your logical, thank you very much. But my problem now is displaying the contents of the cart everytime a new content is added without overwriting the previous content.

I am using this code to display but isn't getting what I want.

foreach( $_SESSION['cart'] as $key =>$value)
{
echo '<table border ="0" width ="100%" cellspacing = "0"><tr><th bgcolor="#cccccc">' . $key . '</th><tr><td>' . $value . '</td></tr></table>';
Post Reply