I'm confused on how to do this. Say the user creates an account with my ecommerce website, and then starts adding products into their basket. If I store the users username and password in a database table, and use sessions/cookies to manage the products in their shopping basket, what would I need to do in order to connect the users shopping basket to their account, so that when they log in they will be able to see the items they had previously stored?
Do I first allow the user to login, query whether they logged in successfully, and then make a session/cookie variable for their username? Or do I have to store the users cart items in a database and connect it with the user accounts table?
I'm confused with how to store a shopping baskets items into a table. Is that even the right way to do it?
There's no code yet, I want to create the databases correctly before I start coding and just need some advice. Thank you
If you have two tables, one for the users and one for the items, you can do something like the following.
Manage current basket items by adding to and removing the item_ids into a serialized array, which you can store in your users table AND the session at the same time, keeping them in sync. For example, if a user visits your store for the first time (not logged in, and empty shopping basket), you can create the session like so.
session_start();
We start the session.
if (isset($_SESSION['current_basket']) {
$current_basket = unserialize($_SESSION['current_basket']);
} else {
$current_basket = array();
}
Because this is the first time our visitor has visited our page, the session variable current_basket will not be set, meaning the above if statement will not run and instead just create an empty PHP array in a variable called current_basket.
Now, when the visitor adds an item to the basket, we just need the item's ID in your database and add it into the current_basket array.
$current_basket[] = $item_id;
Then we immediately update the session variable with the new array, serializing it in the process.
$_SESSION['current_basket'] = serialize($current_basket);
You now have a proper, usable array with all the product IDs for that person's shopping basket.
Now, let's pretend the user was logged in. We'd check for that and only add one more step.
$sql = "UPDATE users SET current_basket=" . serialize($current_basket) . " WHERE id=$user_id"
// Execute that query.
There, now that same array is in the database which you can then pull and set as a session variable when the user logs in.
You can run these series of steps everytime a product is added or deleted, keeping it updated both in the session and the database.
This is obvisouly a very watered down concept for the sake of explaining. Obviously, you'd need to manage the array better. For example, not adding a product to the basket if it's already there, removing items from the array...etc.
The key is to serialize the array, and store in the session always, and in the users table if the user is logged in.
Then when the user comes back and logs in, you can set the array in the session for them using the array in the database from when they were last on your site.
I would have the cart table cart_id / user_id / date_time_created then an item table indexed on the cart_id.
Also add a way to dump the cart_id and items out of the tables after so many hours or days otherwise your database will get huge.
And let your end users know that the items will be dumped after x amount of time.
Related
I'm developing a travel website, I'm wondering if I could store in session or cookies the user searched in my website and use it as value in a query to suggest. Like if the user is interested in hotel categories tourist attraction the suggested for you will show some hotel tourist attraction, I will get the tourist attraction categories from the database and store it in session. I don't have a user login for the viewer. I'm thinking if its possible in PHP? Thank you.
I didn't try yet, I need help.
Yes, it is possible. You can make possibilities like that. When the user entered at least one category, you can set its ID to the cookie or session. After it, you can sort categories from user's saved ID. For example by session:
In category controller:
$_SESSION['category_ids'][] = in_array($category_id,$_SESSION['category_ids'])?:$_SESSION['category_ids'][] = (int) $category_id;
In list page:
$categories = Categories::order(DB::raw(' FIELD(id,'.implode(',',$_SESSION['category_ids'])))->get();
Also, you can set limit of count for category IDs and you can override first IDs with newest.
I am designing as a project a web store (using PHP Laravel and MySQL FYI) and I am at the part where I have to create the logic behind the production system, which goes like this:
-On my Database,
I have 1 ORDER table where I have all the information regarding the shipping, customer, etc.
I have another table called ITEM where are listed all the Items in an order (so if an order has 3 items, there will be 3 lines in the ITEM table with a Foreign Key pointing to the ORDER).
Now I'm creating the PRODUCTION DASHBOARD. Right now I'm able to scan the item ID and get the shipment information on the Dashboard.
After that, for orders with multiple items what I want to do is for the system to tell the user to deposit the item in a numbered box to wait for the rest of the items from the order. That way the user can keep scanning items from other orders and once another item from the ordered stored in X box is produced, he can scan it and the system will then tell him that the other items from the same Order and placed on X box and he can do that until the order is complete.
My question is what would be the best way and logic Database wise (and also Laravel wise if you want to further expand your answer hehe) to implement this BOX system.
I hope my question is clear enough and thank you very much :)
I had a similar system for a project that I was working on. What I did was, was create a database table called temp_orders with a column called items that each item was separated by a line break. Until the order was finalized (100% processed), the order would remain in temp_orders.
Once finalized, it would get deleted from temp_orders and moved over to the orders table. If I needed to check items, I would explode() the data from the items column in temp_orders table using a line break, thus putting them into an array and then using the data however I needed to.
You need to determine when you want to finalize the order. It could be upon credit card payment, or upon user order confirmation, for example.
I'm creating a token based rest api in php for e-commerce application.
Scenario :
Any visitor can add items to cart without logging in. These items are stored in the mysql database, cart table, with user_id value which defaults to 1. // As user is not logged in.
Problem :
After the user logs in, i am able to fetch the userid after decoding the token generated for the user, but want to know, how can i identify which items in cart table belongs to which user so as to update the actual userid against those products ?
Table :
customer_id int(11),
item_id int(11),
quantity int(11),
date_added datetime
Thanks in advance for any help!
You simply can't do it this way. You obviously a field in database which will be the same before, and after the user logs in. For exemple you could try to store an IP address or MAC address when user is not logged in. Then when user is logged in, you search for same values (on IP or Mac address) on the cart table, and set all the matching elements to the user ID.
In anycase, you'll obviously need to store something unique that will make a relation between then cart and the user.
Another solution, maybe the best corresponding to your needs, is to store the cart of unidentified users in cookies. Then, when users logs in, you'll have to browse all items stored in cookies to add them in your database with a correct user ID.
You should generate a (temporary) identification. I'd add an auto incremented column to your database id. After you have created the database you can retrieve the id with something like PDO mysqli_insert_id($conn). Store that id in a PHP session and you have it!
I am trying to understand the database design for an e-commerce site. I am having trouble understanding on what to do in the following situation. Say the user creates an account for the first time, and makes an order. I can write php code which will add the user_id (primary-key), first name & last name. But what if I want to add the user_id in the orders table (user_id in the orders tables is a foreign key). How do I get the value, as the user_id in the customers table is auto incrementing?
Are you using two tables one for storing user details and other for placing orders ?(which is better) If so my recommendation would be to use username (not first name or last name) as primary key and store the username as SESSION variable for each login and for each order add the username in order table along with orders details
There are couple of ways to do that, you can access the newly generated user_id by LAST_INSERT_ID() and use it in subsequent transaction, or you can store it in session and use it later when inserting in Orders table.
I have a website where people can make posts and follow other users. I have a sidebar that has a value that keeps track of the number of posts that have been posted since your last visit.
I'm stuck thinking of how I should handle this. Should I create an entirely new table in the database called notifications that would hold the user's id and the number of posts since last visit, should I just add a column in the existing user table for this value, or should I use an entirely different method?
Thanks.
First of all: Think, which object this is a property of. In your case, the count will differ from user to user, so we might assume, it is a user property.
We could hang it on the last login, but this would give us a wrong count, if the user is logged in for a long period (The user doesn't want to know the count since his last login, but since his last activity!).
So the easiest way could be to add a field to the users table, that holds the last post ID - We just SELECT MAX(id) FROM posts and update users.lastSeenPost with the result on every user action. We can then display MAX(post.id)-users.lastSeenPost as the new post count.
Every post has a date recording when it was made.
Every user will have a date keeping track of when he/she logged in the last time.
By the following SQL statement you could ask the database to return the number of posts since the user logged in last:
SELECT COUNT(*) FROM `posts` WHERE `posts.post_date` > `user.lastlogin_date`
I suggest that you will create a cookie ($_COOKIE['lastPostId']) in each customer webbrowser with the LAST ID of your posts, and, when the user return, you will read $_COOKIE['lastPostId'] and query your database as SELECT * FROM posts WHERE id>lastPostId