Jump to content

Archived

This topic is now archived and is closed to further replies.

jwk811

Carts

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!

Share this post


Link to post
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...

Share this post


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

Share this post


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

Share this post


Link to post
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.

Share this post


Link to post
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..

Share this post


Link to post
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]

Share this post


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

Share this post


Link to post
Share on other sites

×

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.