jwk811 Posted September 4, 2006 Share Posted September 4, 2006 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! Quote Link to comment Share on other sites More sharing options...
corbin Posted September 4, 2006 Share Posted September 4, 2006 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... Quote Link to comment Share on other sites More sharing options...
jwk811 Posted September 4, 2006 Author Share Posted September 4, 2006 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. Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 4, 2006 Share Posted September 4, 2006 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. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 4, 2006 Share Posted September 4, 2006 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. Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 4, 2006 Share Posted September 4, 2006 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.. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 4, 2006 Share Posted September 4, 2006 indeed Quote Link to comment Share on other sites More sharing options...
jwk811 Posted September 4, 2006 Author Share Posted September 4, 2006 thanks, but i wouldn't know how to build one so it would actually work Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 4, 2006 Share Posted September 4, 2006 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]<?phpclass 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] Quote Link to comment Share on other sites More sharing options...
jwk811 Posted September 4, 2006 Author Share Posted September 4, 2006 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.