swissbeets Posted July 10, 2008 Share Posted July 10, 2008 i just built my shopping cart to sell clothing i decided to have it save into a db called cart , cart has these tables cart_id <---- auto increments cookie_id <--- product_id <--------- same as my product id on my products table to be able to get the information related qty <----------- the amount of pieces of clothing they want to buy size <-------------the size of the clothing they would like to buy product_price <-----------price is being saved here and in my products for ease product_name <-----------name is also being saved on both for ease now my question is, does making the cookie id unique for each user and only storing the the cookie_id in a session make sense? also i have no idea how to do this, would i want to generate a random number then i could query with WHERE cookie_id= rand number does this make sense to do it like this? also i am going to be using google checkout and thought it would be easy to just query the db for cookie_id and send it in i have all of this code written but still need to do this last step to make it work for the unique cookie_id any help is greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/ Share on other sites More sharing options...
discomatt Posted July 10, 2008 Share Posted July 10, 2008 Your logic makes sense to me. Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586467 Share on other sites More sharing options...
.josh Posted July 10, 2008 Share Posted July 10, 2008 what are you trying to accomplish with cookie_id? Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586471 Share on other sites More sharing options...
everurssantosh Posted July 10, 2008 Share Posted July 10, 2008 are you using cookiee or session to store your random number? Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586475 Share on other sites More sharing options...
swissbeets Posted July 10, 2008 Author Share Posted July 10, 2008 i thought when u called a session it was storing a cookie....... well i guess i am using a session then, which would be better? this doesnt seem like it should be hard to assign a random number but any ideas on a better way to set this cookie_id? crayon i am trying to make the cookie id be unique for each user so that i can query the cart WHERE cookie_id = (the specific number assigned to the customer) and it will return on the items that this customer added to the cart Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586479 Share on other sites More sharing options...
.josh Posted July 10, 2008 Share Posted July 10, 2008 What are your goals for your shopping cart? Let's say random Joe user comes to your site. He's window shopping and he sees something he might want to buy. Do you want him to be able to add the item to a shopping cart without having to first register/login? If so, let's say that Joe user decides not to buy at this time, so he leaves. Now let's say he comes back later on. Do you want that item to still be in his shopping cart? Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586491 Share on other sites More sharing options...
mbeals Posted July 10, 2008 Share Posted July 10, 2008 good question. Also what is the structure of the 'customers' table? When someone completes the transaction are you storing their personal data? Do they create an account holding their address info? I would probably make that field store a reference to the customer table if it's a logged in or returning customer. If it's not, use the session ID, then store that ID in a cookie if you want the customer to be able to retrieve the cart after closing and reopening the browser. It might also be worth timestamping the entries so you can have an automated script run every night and clean up entries that are xxx hours/days/weeks old. Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586499 Share on other sites More sharing options...
swissbeets Posted July 10, 2008 Author Share Posted July 10, 2008 Crayon, yes that is exactly what i want mbeals, i do not have a customer table, i do not want to require users to log in since i am going to have a checkout (maybe google checkout) just take the information and run the sale itself. how would i timestamp this? i am very new to php so am going to have to lookup all these commands so if anyone knows these off hand it will save me a lot of time could i somehow make the cookie_id be the timestamp? does this seem like it will implement with a checkout easily? thank you very much for your help Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586507 Share on other sites More sharing options...
.josh Posted July 10, 2008 Share Posted July 10, 2008 I don't really advise you to allow a customer to purchase something from you without having to register and login. That just begs for people to scam you. Allowing the user to anonymously add items to a shopping cart is a convenience for them and is okay for you to do. Letting them come back later anonymously and still having their cart info is also a convenience, but it isn't 100% accurate. For initial anonymous shopping, you can use their uniquely generated session id to keep track of their shopping cart. For follow-up anonymous shopping, you can store their session id in a more permanent cookie and check if it's there, but the user could have cleared his cookies since then, so it's not accurate. You could also store their ip address in a db table and search and re-associate with that, though again, that's not 100% accurate, because ip addresses can change. A better method is to have a "Save my cart" type of button, which would require the user to register. It can be minimal, just requiring an email address and choose a password (allow them to fill out all info on form, of course, only make those 2 the only 2 mandatory fields at that stage). That way, you can store that info in your db and associate items with those things, and the user can always login anytime, anywhere, and access their cart. Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586514 Share on other sites More sharing options...
mbeals Posted July 10, 2008 Share Posted July 10, 2008 there are several ways to time stamp. MYSQL has some timestamping functions, but I would just use time() to get the current time stamp and then add it to the insert statement. Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586519 Share on other sites More sharing options...
swissbeets Posted July 10, 2008 Author Share Posted July 10, 2008 well i do not want to have this information save for very long anyway because my database could get too large, storing their ip address as the cookie seems like it would be the best way for me to do it, should i store it as a cookie or a session? i have been working on this shopping cart for so long now, everything is making sense though right? i had to build it myself because i could not find one that i thought would work thanks Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586522 Share on other sites More sharing options...
DarkWater Posted July 10, 2008 Share Posted July 10, 2008 Actually, mbeals, you should just use the MySQL NOW() function. Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586523 Share on other sites More sharing options...
rmbarnes82 Posted July 10, 2008 Share Posted July 10, 2008 Hi, I would just store the customers cart_id in the session like this: $_SESSION['cart_id'] = $cart_id; Then on each page load just use the created session variable to retrieve the cart_id, then based on this load the cart from the database. This of course means that if the user's session times out (usually after like 15 minutes of inactivity on most systems), the cart gets emptied. I just think you have to live with that if you don't want them having a user account. Also, I don't mean to throw a spanner in the works, but you do realise that the cart table only allows for one product per cart, right? Robin Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586527 Share on other sites More sharing options...
swissbeets Posted July 10, 2008 Author Share Posted July 10, 2008 yes, that is why i want to have the cookie_id, so that i can query the cookie_id, not the cart_id the cart id is kind of useless actually.... but i have got something like this so far function getcookieid() { $cookie_id= $_SESSION['cart']; if (!isset($cookie_id)){ $cookie_id = time(); echo $cookie_id;//this is just to make sure it is working $_SESSION['cart'] = $cookie_id; }return $cookie_id; } but when i call it on my shopping cart page, getcookieid(); echo $cookie_id; i get nothing Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586534 Share on other sites More sharing options...
mbeals Posted July 10, 2008 Share Posted July 10, 2008 Actually, mbeals, you should just use the MySQL NOW() function. I hate using now() and all mysql date functions because of the way it stores the date. Maybe I'm just old school, but I prefer to work with normal old unix time stamps. I know my code will explode and cause a rift in the space time fabric if I attempt to insert a date before 1970, but for this type of app, that doesn't seem to be an issue. Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586547 Share on other sites More sharing options...
swissbeets Posted July 10, 2008 Author Share Posted July 10, 2008 i changed it so its not a function but am getting an error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Copy (3) of website\shoppingcart.php on line 67 $cart_set = get_cart($cookie_id); echo $cart_set; while($row = mysql_fetch_array($cart_set))<----line 67 this is the function get_cart() function get_cart($cookie_id) { global $connection; $query = " SELECT * FROM `cart` LIMIT 0 , 30 WHERE 'cookie_id' = ".$cookie_id; confirm_query($query); $cart_set = mysql_query($query); return $cart_set; } any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586622 Share on other sites More sharing options...
DarkWater Posted July 10, 2008 Share Posted July 10, 2008 $cart_set = mysql_query($query) OR die(mysql_error()); Please post the MySQL error you recieve. Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586631 Share on other sites More sharing options...
swissbeets Posted July 10, 2008 Author Share Posted July 10, 2008 it is saying that the query is empty...? which i am assuming means the problem is in the function? Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586638 Share on other sites More sharing options...
DarkWater Posted July 10, 2008 Share Posted July 10, 2008 What does confirm_query() do? Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586641 Share on other sites More sharing options...
swissbeets Posted July 10, 2008 Author Share Posted July 10, 2008 function confirm_query($result_set) { if (!$result_set) { die("Database query failed: " . mysql_error()); } } i changed the query to this and still doesnt work function get_cart($cookie_id) { global $connection; $query = " SELECT * FROM `cart` WHERE cookie_id = ".$cookie_id; $cart_set = mysql_query($query); return $cart_set; } Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586647 Share on other sites More sharing options...
swissbeets Posted July 10, 2008 Author Share Posted July 10, 2008 i found the problem but dont know why it is happening with this code, $cart_set = get_cart($cookie_id); echo $cookie_id; echo "<br/>"; echo $cart_set; echo "<br/>"; $cart_set = mysql_query($query) OR die(mysql_error()); by changing the function to function get_cart($cookie_id) { global $connection; $cart_set= " SELECT * FROM `cart` WHERE cookie_id = ".$cookie_id; return $cart_set; } i get this 1215709849 SELECT * FROM `cart` WHERE cookie_id = 1215709849 Query was empty when i put that SQL into my mysql client IDE it returned fine, how could this be? Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586654 Share on other sites More sharing options...
swissbeets Posted July 10, 2008 Author Share Posted July 10, 2008 dont know what the problem was, but solved it with this function get_cart($cookie_id) { global $connection; $query= " SELECT * FROM `cart` WHERE cookie_id = ".$cookie_id; $cart_set = mysql_query($query); return $cart_set; } after this though i need have the database empty after a certain amount of time, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586681 Share on other sites More sharing options...
DarkWater Posted July 10, 2008 Share Posted July 10, 2008 Store the time that the cart was created in the database, then run an hourly cron job to delete any that are over like, 3 days old. Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586687 Share on other sites More sharing options...
swissbeets Posted July 10, 2008 Author Share Posted July 10, 2008 is that guna be SQL or php? and the cart id is already saving the time so that shouldnt be bad Quote Link to comment https://forums.phpfreaks.com/topic/114108-logic-question/#findComment-586792 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.