Jump to content

PHP watch list


Pain

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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'

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.