Jump to content

logic question


swissbeets

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;


		}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

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.