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
https://forums.phpfreaks.com/topic/19645-carts/
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
https://forums.phpfreaks.com/topic/19645-carts/#findComment-85575
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
https://forums.phpfreaks.com/topic/19645-carts/#findComment-85599
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
https://forums.phpfreaks.com/topic/19645-carts/#findComment-85783
Share on other sites

Archived

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

×
×
  • 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.