Jump to content


Photo

Carts


  • Please log in to reply
9 replies to this topic

#1 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 04 September 2006 - 06:30 AM

Is creating shopping carts an easy thing to do? I don't really have a clue but figured it could be done with sessions with php so they wouldn't have to create a log in account because i couldn't get the accounts thing down had too many problems with connecting to database and could never solve them. Any help would be great!

#2 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 04 September 2006 - 06:39 AM

I've been working on a cart system for the past couple days... And i started out using sessions and arrays... But found that after adding and removing a lot of items that can become cluttered and so on... So, I just use the session id as the thing I use in the mysql query such as SELECT * from `items` where `sess_id` = '{$sess_id}';... But it can be done with sessions alone...
Why doesn't anyone ever say hi, hey, or whad up world?

#3 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 04 September 2006 - 07:04 AM

I really don't understand that stuff. Is there anyway you or anyone could put that simpler and show me how to do that, because I am still just a beginner.

#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 08:47 AM

everytime user clicks 'add to basket,' add it to the session data and/or table.

everytime user clicks 'remove from basket,' remove it from the session data and/or table.

Baskets are no different from your standard Order tables/pages.

#5 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 04 September 2006 - 08:53 AM

Carts are one of those things where I think its worth the hassel to go teh whole hog...

I use sessions and cookies - don't bother with the database tables.

Reason for this is if the user disconnects for what ever reason. If you use the session id as an identifier for the database record all will be lost when they come back - and people can find that incredibly annoying.
follow me on twitter @PHPsycho

#6 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 09:09 AM

or you can just be sensible and not use the session ID as the key on your basket table.. if the user accidentaly disconnects after adding a large number of items to a cart they are going to be mighty annoyed to find it all lost when they return..

#7 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 04 September 2006 - 09:15 AM

indeed

follow me on twitter @PHPsycho

#8 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 04 September 2006 - 03:06 PM

thanks, but i wouldn't know how to build one so it would actually work

#9 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 03:36 PM

Dude, Think about it..

Everytime a user clicks the add to basket button, your code need to add that product..

So therefore you already know that you'll be inserting a record, or adding another item to an array, or writing another line to a flat file etc.

Vice versa for removing the item.

A very quick untested example:
<?php

class Basket
{
    private $userId;
    private $db;

    public function __construct (DataBaseClass $db, $userId)
    {
        $this->db = $db;
        $this->userId = (int) $userId;
    }

    public function addItem ($prodId, $quantity)
    {
        try
        {
            $this->db->parseQuery
            (
                'REPLACE INTO `basket` (`userId`, `prodId`, `quantity`) '
               .'VALUES(:1, :2)'
               ,$prodId
               ,$quantity
            );
            $this->db->commit();
        }
        catch (DataBaseException $e)
        {
            throw new BasketException('Error adding item');
        }
        catch (Exception $e)
        {
            throw $e;
        }
    }

    public function removeItem ($prodId, $quantity)
    {
        try
        {
            $this->db->parseQuery(
                'SELECT `quantity` '
               .'FROM `basket` '
               .'WHERE `prodId` = :1 '
               .'AND `userId` = :2'
               ,$prodId
               ,$this->userId
            );
            $this->db->commit();

            $quant = (int) $this->db->fetchField('quantity');

            if ($quant <= $quantity)
            {
                $this->db->parseQuery
                (
                    'DELETE FROM `basket` '
                   .'WHERE `prodId` = :1 '
                   .'AND `userId` = :2 '
                   ,$prodId
                   ,$this->userId
                );
            }
            else
            {
                $this->db->parseQuery
                (
                    'UPDATE TABLE `basket` '
                   .'SET `quantity` = `quantity` - :1 '
                   .'WHERE `prodId` = :2 '
                   .'AND `userId` = :3'
                   ,$quantity
                   ,$prodId
                   ,$userId
                );
            }
            $this->db->commit();
        }
        catch (DataBaseException $e)
        {
            throw new BasketException('Could not remove item.');
        }
        catch (Exception $e)
        {
            throw $e;
        }
    }
}

?>


#10 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 04 September 2006 - 03:59 PM

I am just a beginner when it comes to php and thought I'd be able to do this myself. I think I'm going to have to work with ecommerce because this is very hard for me. Thanks for tryin'  ;D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users