Reputation: 29
I'm developing a software portal, where users can add a software as favorite and when they do that, the software will get displayed in their profile.
For doing this, I planned to do the following:
There is a separate column named "favorites" in the "users" table. When a user adds a software as a favorite, then the software ID will get added using comma, like
"102,405,502,695,595"
For displaying I planned to use PHP's explode function and query the software name after that.
My question is: what happens if the user has some 100 favorites? Does it take long time to load the list? Is there any other method to implement this concept?
Upvotes: 2
Views: 1931
Reputation: 49
I guess you don't want to limit your users to a maximum number of favorites?
Anyway, i such situations, i create an additional database-table, lokking somehow like
Table FavoritesUserRelations (int)user_id | (int)software_id
This approach will make your code simpler, if you're going to work with an Fav-Entry, like deleting a Favorite, adding a new one or sorting the for displaying purposes. Also, you're not limited in size, maximum or search statements ...
Upvotes: 0
Reputation: 53196
That is a horrendous was of doing it.
You must normalise your tables. If you don't know what normalization is, then you need to read up on it before you start this project.
Basically, you will need three tables.
Users will have id
, username
Software will have id
, softwareName
,
UsersFavouriteSoftware will have a "registry" where you have the join relations.
Users Table
id | userName
1 | Bob
2 | Alive
Software Table
id | softwareName
1 | Firefox
2 | Chrome
3 | Internet Exploiter
Join Table
userId | softwareId
1 | 2
1 | 3
2 | 1
So user with ID 1 likes Chrome and Internet Exploiter, and you can get this information with a simple join query.
Upvotes: 0
Reputation: 39724
From my point of view it's not ok, not because you have to explode that list every time, because that's ok.
The problem is that every time someone deletes 1 favorite the script has to fetch all that text, explode, remove that id and update again.
You should make that table like id|user|id_software
, every software with it's own row, then selects, updates and delete will be much faster and easier.
For your answer: nothing happens if you have 1000 favorites, PHP is designed and capable to handle huge amount of data if settings, machine and OS are ok.
Upvotes: 3
Reputation: 2959
Youu can use another table to assign the relationship between user and Software. To use such serialized things in one table is good when there are only a few entries as you noticed.
Upvotes: 1