Jump to content

Carts


jwk811

Recommended Posts

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

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

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

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

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:
[code]<?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;
        }
    }
}

?>[/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.