|
-
Aug 18th, 2003, 10:46 AM
#1
Thread Starter
Lively Member
cookies for virtual store [RESOLVED]
in every attempt to explain what is a cookie, people give the example of a virtual store,
well I happen to write a virtual store script and I am in the part of letting the customer to buy more then one item.
I need to use cookies to store all the data.
I have this database that contain all the items for sale, and I have the page that shows them.
I need to add near every item a small shopping cart that when the user clicks on it its storing the item name, and item price in the cookie.
the problem is I dont know how to do that when the customer clicks on a cart and then on another cart it will not erase the last data that was in the cookie.
in other word I dont understand how to use the cookies to store this data.
anybody can help me by pointing the direction of how to do it?
Yair
Last edited by yair24; Sep 8th, 2003 at 01:13 AM.
-
Aug 18th, 2003, 11:03 AM
#2
Frenzied Member
the onnly way you can overright a cookie is if you stored the cookie with the same name. or you deleted the cookie first. jsut watch teh name of the cookie so you don't overwrite it.
-
Aug 18th, 2003, 11:07 AM
#3
Thread Starter
Lively Member
you mean that everytime the user click on a shoping cart I create new cookie with new name?
like
setcookie(item1="bla" item2="bla2" item3= "bla3")???
but what hapened if the customer buy 1000 products? will it be a big big bgi string?
this is the way to do it?
-
Aug 18th, 2003, 11:11 AM
#4
Frenzied Member
or you load the cookie that htye have presently and then jsu tadd it to the original cookie.
say they bought something and you wrote a cookie. if they bought another item then you would load that cookie and store it in another varaible. you would then add both items to the same cookie. this way you have 1 cookie and a list of items in that cookie.
-
Aug 18th, 2003, 12:40 PM
#5
Stuck in the 80s
Personally, I'd store all this data in a database. I'd generate a random customer ID for them, or if they are registered, they would already have one.
Each time they selected an item, it would add a record to the database, something like:
Code:
ID CustID ItemID addDate
4 124332 443322 11/22/04
When they view their shopping cart, you'd simply get their customer ID from their cookie and grab all items matching it from the database.
If they are not a member, you can also setup the script so that it will delete all records in the database older than 30 or so days.
I think that would be a much better method than storing a lot of text in a cookie.
-
Aug 19th, 2003, 01:38 AM
#6
Thread Starter
Lively Member
Originally posted by phpman
or you load the cookie that htye have presently and then jsu tadd it to the original cookie.
say they bought something and you wrote a cookie. if they bought another item then you would load that cookie and store it in another varaible. you would then add both items to the same cookie. this way you have 1 cookie and a list of items in that cookie.
you know what?
I totaly agree with you...
but before I do that I have another question:
I succeeded to put data in arrays in the cookie,
I put only the product name in the cookie and everytime I incremented the array by adding another product to it.
the problem is that I dont have only product I have also amount:
suppose the customer want to buy 3 apples then how can I store in the cookie this data:
product=apple , amount=3
in C I would probably make a struct like this:
struct cartItem
{
char productName[20];
int amount;
}
and then I would have put it in an array of cartItems like this:
cartItem[20];
if there is a way to do such a struct in PHP then I prefer to use cookies, if not I will change the method to the temporary database as the hobo suggested.
Yair
}
-
Aug 19th, 2003, 08:24 AM
#7
Frenzied Member
that could get tricky. you could have the cookie 1 name but the value could be a lot as they could be seperated by a semi-colon or colon. then you would just have to seperate them at the semi-colon and then do what you want with them. I agree with hobo, that is the best way to go also.
-
Aug 19th, 2003, 08:41 AM
#8
Thread Starter
Lively Member
this is what I need
I actualy prefer the method with the cookies...
because the database method is problematic for example:
the table that contains the choosen items actualy contains all the customers items and not just one customer items so because of that you need an SQL query to choose only the items that customer ID X (for example) has chossen.
imagine if you have many many customers, the database will have so many records and the query will go slow...
I mean all the customers can afect each other
(the more customers there are, the more slowly it will take to the page showcart.php to load)
while in the cookies method it doesnt matter how many customers there are the showcart.php page will have to read only one customer's data for each customer. so there could be milions of customers and each of the customer have their data in their cookie and the page will read only their cookie...
I hope I explain my point clearly.
BTW is it a good point? or its nonsense?
Yair
-
Aug 19th, 2003, 12:17 PM
#9
Stuck in the 80s
Re: this is what I need
Originally posted by yair24
I actualy prefer the method with the cookies...
because the database method is problematic for example:
Databases are not problematic. They are a convenience.
Originally posted by yair24
the table that contains the choosen items actualy contains all the customers items and not just one customer items so because of that you need an SQL query to choose only the items that customer ID X (for example) has chossen.
imagine if you have many many customers, the database will have so many records and the query will go slow...
You obviously don't understand MySQL very well. It will not go slow with many records.
Originally posted by yair24
I mean all the customers can afect each other
(the more customers there are, the more slowly it will take to the page showcart.php to load)
Wrong.
Originally posted by yair24
while in the cookies method it doesnt matter how many customers there are the showcart.php page will have to read only one customer's data for each customer. so there could be milions of customers and each of the customer have their data in their cookie and the page will read only their cookie...
The cookie method seems tacky to me. I personally wouldn't store more than 255 or so characters in a cookie.
If your customer has 100 or so items, that's going to be a big cookie.
Originally posted by yair24
I hope I explain my point clearly.
BTW is it a good point? or its nonsense?
It's not nonsense, I just don't agree with it. But that's just my opinion.
-
Aug 19th, 2003, 12:32 PM
#10
Thread Starter
Lively Member
do you mean there is no different at all wether there is 1000000 records in a database then if ther is only 10 records in the database?
-
Aug 19th, 2003, 01:34 PM
#11
Stuck in the 80s
Originally posted by yair24
do you mean there is no different at all wether there is 1000000 records in a database then if ther is only 10 records in the database?
Yes, there is a difference, but it is not going to be that much.
What if your visitor doesn't have cookies enabled? Using a database, they would still be able to shop. They would just have to entered their user ID each time they add an item or check out.
What if their hard drive crashes? When they get a new one or reformat, they will have to re-add everything to their cart.
I guess this wouldn't be a problem if you're just going to cater to people who want to buy on the spot.
But what if someone comes to the site, sees a bunch of stuff, and adds it all to their cart and wants to wait a few days until they get their pay check and can purchase?
There's hundreds of IFs, and I think using a database is the best answer to most of them.
-
Aug 19th, 2003, 07:45 PM
#12
Frenzied Member
yair24, hobo is correct. you shouldn't worry about how many customers you have. a database wil hold them all. if you code right you can query just the selected person so it doesn't matter how many users you have. it will query only 1 person per customer.
very valid point on some users don't have cookies enabled. do not rely on them having cookie enabled.
nobody said you had to keep all those customers in the database once they are done. you can always delete them when they are done.
-
Aug 21st, 2003, 10:21 AM
#13
Thread Starter
Lively Member
ok then,
I will use the database method, after all... 
Yair
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|