Pain Posted February 20, 2012 Share Posted February 20, 2012 Hi there. I am building an online shop for my uni project and i want to make something like ebay's watchlist. However i'm not sure what whould be the best option to store watchlist information. I have two tables so far - users and products. If you have an idea how could i implement this, please share:) Thanks for your help. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 20, 2012 Share Posted February 20, 2012 Just a simple table with two columns for the user and product foreign keys should suffice. Quote Link to comment Share on other sites More sharing options...
Pain Posted February 21, 2012 Author Share Posted February 21, 2012 So if i do that, then there will be multiple rows with the same username (if a user has multiple items on watchlist). Is that really ok? By the way, why foreign key? Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 21, 2012 Share Posted February 21, 2012 So if i do that, then there will be multiple rows with the same username (if a user has multiple items on watchlist). Is that really ok? Yes, absolutely By the way, why foreign key? No offense, but you must not understand how relational databases work. Tables of records will routinely have a "primary" key which is the unique id for that record. Then in other tables where you have records that must be "related" to those record a "foreign" key will be used. The foreign key will be the primary ID value from the other table. Quote Link to comment Share on other sites More sharing options...
Rifts Posted February 21, 2012 Share Posted February 21, 2012 why not just add a row to the users table called watching or something. When the user clicks watch it will pull the array from the db and check it to see if its already being watched if its not add it to the array and restore the array? Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 21, 2012 Share Posted February 21, 2012 why not just add a row to the users table called watching or something. When the user clicks watch it will pull the array from the db and check it to see if its already being watched if its not add it to the array and restore the array? Because storing data in that way takes away your ability to easily do queries based upon many different scenarios. For example, if you wanted a list of all the products a user was watching, it would require two queries instead of one. And, if you wanted a list of all the users that were watching a certain product it would be even more complicated. Don't go adding arrays to database fields because you don't know how to properly use a database. Using an intermediary table, this would give you a list of all the products watched by a user SELECT products.* FROM products RIGHT JOIN watched_products USING (product_id) WHERE watched_products.user_id = '$userID' 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.