Friends table for a social network website - php

Now i am working on a social network website and i already built the friends table for this website but i need some suggestion before i moved forward.
I have a user table where user_id is the primary field.
So for friends i made a friends table with fields like
friend1,friend2,is_active
what i do actually when user1 send a friend request to user2 then at that moment i insert the two rows into the friends table like
1- friend1->user1,friend2->user2,inactive
2- friend1->user2,friend2->user1,inactive
So when somebody accept the friend request at that moment i made the both rows as active.
I made this two entries as i want whenever one user block another then easily made that corresponding row belongs to that user will be inactive.
And if a user remove the user then at that time i delete the both entries.
So please i need some suggestion regarding this please help me out to solve this asap.
I need to know is there any other way which will be more optimized than this and giving proper result.

I think that
Friends_table
with
User_id1, User_id2 and Rleationship_id
is easier, index on both first and second column.
relationship id will be 1 - id1 request, 2- id2 request, 3 - friends, 4- id1 blocked, 5 - id2 blocked...
just an idea.

Related

mysql how to allow user to update a column in a DB once with php

So I have two different tables, a users table and an articles table. The idea is to allow a user to rate an article, but only allow them to rate it once (possible change their existing rating too but I can come to that conclusion later).
As of now I just have the update value working to allow them to rate the article, but of course a user can rate an article as many times as they want.
To give you an idea of how I have everything working, when a user logins in, a session is created with their user information. So when they go to rate an article, I have the ability to check the user, I just don't know how to stop them from rating if they have already rated a specific article.
The user table consists of among other things their username and their unique ID
and the article table consists among other things the article contents, the article unique ID, and the articles rating.
I had some really sloppy ideas like when the user rates an article their ID gets stored into the articles row in some kind of "users who have rated" column, and then I can do a for loop or something to siphon out all the user IDs and then check if their ID exists in that articles entry but then each article would have a row with possibly hundreds or thousands of userIDs on it and there seems like there would be a more elegant way.
Any help or direction is appreciated :)
Create a UserRatings table which has foreign keys to the users table and the articles table, and stores a row linking the user to the article, and the rating they gave it and when it occurred.
Then if a user tried to rate it again you just check this table for the user ID/article ID combination before allowing it.
And then if you wanted got can do things like show the user a list of articles they have previously rated, etc

How to create a query that searches different tables on the same database to log a user into a website?

I'm building a simple login/registration feature, and I'm having a little trouble.
The issue is this, the user system I'm designing is supposed to accommodate different types of users, like (Blue users, Red users, Black users etc). So I was considering porting their different user data to separate database tables, and even giving them separate registration pages, because the content they would view on the site would vary depending on their color.
For example:
Blue Users:
INSERT 'username' INTO `blue` where...
Red Users:
INSERT 'username' INTO `red` where...
But I want to know if there's a way to log them into the site from the same login page without resorting to sending them to different pages when they want to login. I tried the following:
"SELECT `id` FROM `blue`,`red` WHERE `username`...
but it did not work, so I'm asking if there's a way to register the different user types on the same page and log them in on the same page while still giving them their different content.
One problem is probably that the "id" value is an auto-incremented primary key, right? Therefore it would be unique to only the table that they are in, but not over all tables (blue, red, black)
You would then always have to make sure to have unique usernames and ids over all groups or let them select what group they are when they log in and then only load data from that table.
If you have already solved the uniqueness problem, you should be able to simply use the union querys already suggested by other users. You can't use blue, red, because that is short for blue JOIN red, which of course will only result in data where columns with the same name are equal across blue and red.
Assuming that the user can be just in one table, you could use UNION...
SELECT id FROM blue WHERE username...
UNION
SELECT id FROM red WHERE username...
... but as a piece of advice, if user info is the same for all, you better have one table for usertypes and another for users, and link them with a foreign key...
USERTYPES TABLE
id_usertype
usrt_name
USERS TABLE
id_user
id_usertype (this should be a foreign key)
usrname
I hope it helps.

Query and Display referrer details

I have a website built with PHP and mysqli and now I am building administration panel for this website. I am facing some difficulties for querying and displaying referral data I do not have any idea how to do it.
I have a table called user_registration and the fields include, user_name, email, password, referrer. Whenever any of the registered member is referring others the referrer username will be saved in the field "referrer".
Now what I want is to fetch only the rows of the members who has referrals (means referred by my registered members) and also want to count how many referrals a member have and echo it.
For example: I have 20 registered members and from it 5 members have some referrals so I want to query and echo those member's username who has referrals and also count how many referrals they have:
Member's Username Total Referrals
user7 8
user6 6
user1 5
user9 3
user5 2
My solution would be. Make a new column in your user table like "totalreferrals". In this way you can easily keep track of the amount of people they have referred. You could also make a new table "referral". In here you would save the referrals name, the new persons name and a timestamp or something. Now it is even possible to make queries for different periods.
I think that you need to rethink about your database setup, instead of trying to make way to complicated queries, to make your database work.
I am just giving an idea in short which may help you to complete your project -
Suppose your site URL is http://yoursite.com.
1) Whenever a user is registered, you can generate a random key for that user and save it in the database for that particular user. e.g. user1 => abc123
2) Now, you can tell your user (may be on a page after their successful registration) that his/her referrer id is http://yoursite.com/?ref_id=abc123 (by appending it as a new parameter)
or if your register page is http://yoursite.com/register then http://yoursite.com/register/?ref_id=abc123
3) Now, they can share this link to their friends, colleague etc to whomever they want.
4) Now, suppose if a person (may be his friend to whom the user1 referred) clicks on the above link. He will land on your site with the parameter 'ref_id' appended.
5) Just using $_REQUEST['ref_id'] or $_GET['ref_id'], capture its value and store it in a SESSION variable. This you can do on the main page of your site e.g. index.php
6) Now, if that user does the registration, you can make an entry in the database that he has been referred once by the user who has referrer id abc123 i.e. user1.
7) So, you can add count = 1 for user1. If more people come with the same value for the 'ref_id' parameter, you can keep on increment the count.
8) If no such parameter is exists when user lands on your site, then that means he has not referred by anyone. So, that means he is the first kind of user i.e. user in point no. 1).
9) You may also need to take care of some validation part at some places in this.
EDIT:
SELECT user_name, referrer FROM user_registration WHERE referrer > 0;
Assuming that you have a proper insert query ready which gives you the result as you shown the table in your question.

MYSQL - insert into table or update if anything is different

Okey, so im trying to store the logged in facebook userĀ“s friends list into a MYSQL table.
Im storing the logged in user data: ID and Name;
And am storing the logged in user friends list: ID and Name;
So the table looks like this:
['facebook_user_id'] ['facebook_user_name'] ['facebook_friend_id'] ['facebook_friend_name']
123123123123 User1 23424234234234 User 20
231321231231 User2 23424234234234 User 20
So you can see that user1 and user2 has the same friend and if user friend 'User 20' change his/her name I need to update the information, but only for the logged in user, so if user1 logs in the effect will only effect him/her.
But if user1 gets a new friend I need to insert the new friend and update if theres any changes.
How should I do this with PHP? I have seen something similar but didnt work that well for me.
INSERT INTO table (facebook_user_id, facebook_user_name, facebook_friend_id, facebook_friend_name)
VALUES ('facebook_user_id' (to short it down) .....)
ON DUPLICATE KEY UPDATE (facebook_user_id=facebook_user_id (and for every entry)....)
Thanks in advance, and any pointers will help me out alot.
I think this is what you meant.
I am trying to save user A Facebook friends into a table.
When someone logs in with Facebook I am storing, their name and Facebook id. As well as their friends, Facebook id and name.
This is how I would do it. Here is my solution if you need generated schema code let me know.
-Create three tables: registered users, buddies, lookup table
table 1: registered users
[id, userId, facebookUid]
table 2: buddies
[id, buddyid, buddy name]
table 3: You need a lookup table.
[id, userId, buddyid]
Steps:
When UserA logs in with Facebook, save their buddies into the buddies table
This approach above you don't have to worry about duplicate names. If someone updates their name, it will be changed across the board.
You can use join to find who has buddies in common.

CakePHP allow users to add other users as friends and group them

I am building a friend system in CakePHP for users based on this: Add a friend feature in CakePHP
But I need to add two additional features:
1.) Users need to be able to request and confirm/cancel friend requests
2.) Users need to be able to group friends e.g. Friends, Family, Co-workers etc
Can anyone give me some ideas as to what extra tables I would need and the likely relationships between them and the existing tables.
Also based on that article it seems the relationships are based on which user asked who but I don't care much for that so what would I do to make it so it's just a relationship rather than tracking which user requested the friendship and who is the friended friend?
NOTE: I asked a similar question earlier and had to delete it as everyone mis-understood the question. I am asking a question just like the one mentioned above and hoping to get answers like they have in terms of tables and relationships.
You'll need a field in your relationships database table called confirmed or similar. Basically when a friendship is requested a record is created, but with confirmed set to 0. When the "requestee" accepts the friendship, the confirmed field is set to 1, or if declined the record is deleted (allowing the requester to request friendship again).
To group, you'll obviously need a groups table with an ID and non-unique name field, and a groups_relationships table that has two columns: relationship_id and group_id. This will allow a user to place a friend into a group. You can then query the groups_relationships table with a group_id to find the IDs of friends that a member has placed in that group.

Categories