Reputation: 31
I'm new in programming. I hope somebody will help me out with this one.
I have a website with user profiles. Users can simply write articles. Currently all articles from every user are displayed in any profile page and I don't need this. I want to do something like "link" articles with users who made it.
For example.. if I watch somebodys profile I see only articles made by this user or I watch my own profile I see only my posted articles.
So, if someone can tell something about staff like this, please leave a reply with info or code example.
Thanks!
Upvotes: 2
Views: 1310
Reputation: 7759
Currently you would have a table in your mysql database for a User, and a separate table for an Article (I hope).
What you need to do is modify your Article table so that it has an integer field which represents the user that created that article. This is what is called a foreign key (you may also want to read about MySQL foreign keys.
Then, to find the user that created a particular article, you can use a SQL SELECT
statement on your User table to find only the users where the user id is equal to the user id stored in the article. Similarly, you can 'reverse' this to find all Articles authored by a particular User.
Upvotes: 2
Reputation: 4316
You'll need to set up your database tables to have some relational quality to them. For example, when users register, they should be assigned some sort of unique ID, and then when a new article is added to your table containing all the user-created articles, you should assign the authoring user's unique ID to that particular article row. This will set up a natural SQL join for you, upon which you can retrieve all articles for the user with the ID 1234 by simply selecting all articles from the article table with that ID.
Upvotes: 1