PDA

View Full Version : Shopping cart help !!!


wap3
11-18-2002, 12:13 PM
Hello

I am trying to learn php etc and so setup a database on my site. I have managed to set-up the database and generate all the relevant pages. I am however struggling on making a shopping cart. I have tried looking at some tutorials but can't work it out. I have a php generated page which lists items in the database and for each of those item, they all have a button with a unique ahref tag. e.g add_to_basket.php?Item_id=56

Now if i click on that button for example how can I get it to store that in a session for later use. This is all I have to start this off. I would appreciate some help with this. Some pointers etc or maybe a little bit of code.


<?
include ('cart_fs_library.php');

// The shopping cart needs sessions, so start one
session_start();

if(!session_is_registered("cart"))
{
$cart = array();
session_register("cart");
$items = 0;
session_register("items");
}
if($cart[$Item_id])
$cart[$Item_id]++;
else
$cart[$Item_id] = 1;

if($cart&&array_count_values($cart))
display_cart($cart);
else
{
echo "<p>There are no items in your cart";
echo "<hr>";
}


This is abit of a mock-up which I have kind of gathered from various places. Is this any good ??

Any help appreciated

:thumbsup:

wap3
11-20-2002, 01:25 PM
?? Ok

Here is another question. How can I print the contents of the session out quickly just so I can see if it works and has stored anything ??


Thanks :confused:

Spookster
11-20-2002, 07:11 PM
Personally I would utilize PHP's object oriented capability and create a shopping cart class. Here is a simplistic example from a previous thread:

http://codingforums.com/showthread.php?s=&threadid=2375

wap3
11-20-2002, 07:45 PM
Ok thanks,

do you know of any tutorials about these with abit more information ??

Thanks

wap3
11-20-2002, 08:32 PM
Ok so im trying to work with that class system. The following is what I have so far in the same page. I am calling this page with a link on another page which is like this cartpage.php?new=56

Thats where I get the $new from hopefully. As you can see im trying to add that to the items array then print out the array contents so I can see if its working.

All I get for printout is "Array" thats obviously not working. Can somebody tell me whats wrong ??

Thanks :confused:


<?
class MyShoppingCart{

var $items = array();

function add_item($new) {
if ($this->items[$new]){
$this->items[$new] = $this->items[$new] + 1;
}
else {
$this->items[$new] = 1;
}
}

function delete_item($new) {
$this->items[$new] = 0;
}

function display_cart() {
$content = $this->items;
echo "$content";
}
}

session_start();
$cart = new MyShoppingCart();
session_register("cart");

$cart ->add_item($new);
$cart ->display_cart();

?>

wap3
11-22-2002, 02:21 PM
Ok so I have gone back to my first method for now cos I seem to be making more headway.

I have added some bits to the script.
If a click a link which goes to the cart it will add that, then if I click back then add something else it adds that. But then if I try it again it overwrites the last one I did but leaves the first.
This is what it ouputs:

Array ( [cart] => Array ( [64] => 1 ) ) Array ( [61] => 1 )

Looking at it I would think it should be continuing to add items to the first array not like it is ?? Any ideas on why this is ??

This is the script I have now:



<?
session_start();
// The shopping cart needs sessions, so start one

//new item selected
$new = $_GET['new'];
if(!session_is_registered("cart"))
{
$cart = array();
session_register("cart");
}
if($cart[$new])
$cart[$new]++;
else
$cart[$new] = 1;

if($cart&&array_count_values($cart)) {
print_r($HTTP_SESSION_VARS);
print_r($cart);
}
else
{
echo "<p>There are no items in your cart";
echo "<hr>";
}

// if we have just added an item to the cart, continue shopping in that category
?>