How to implement the function "Add to friends" in PHP? Scheme just throw in!
2 answers
To begin with, think over the structure of the tables, it is obvious that there should be two tables, one with users, the second with friends. Many to many it is called. You create them, and in pkhp then you just take them to the table with your friends and add the id_user and id_drug. Id_druga is the same id_uzer from the table with users.
1st Table with users:
`users` `id` `name`
2nd Table connection with friends (I use. Many to many):
`friends` `user_id` `friend_id`
Now friend_id
is the id
any user from the 1st table.
And in php
business. For example
// берем ид-шник по имени поль-я $UserId = getUserIdByName($UserName); // берем ид-друга, так же само по его имени, $FriendId = getUserIdByName($FriendName); // Добавляем в таблицу friends связь function AddToFriend( $UserId, $FriendId );
Imagine that we have in the table with users two users Вася
[2] and Петя
[3], well, actually you [1] should also be there, i.e. 3 persons.
And we proceed further as I described above, first take your id, then id, say Васи
, and add them to the Friends table. What is the result?
You should end up with something like the following in the friends
table.
`user_id` | `friend_id` 1 | 2
To add Петю
, we act in the same way, take each other’s IDs and add them to the tab with friends, in the end we’ll get the following data set:
`user_id` | `friend_id` 1 | 2 1 | 3
Beautifully turned out? And now it's very easy to choose friends by your id
, but you do it first.
- Thank you very much! It would always be like that :) I have one more problem to understand: hashcode.ru/questions/147821/php- output- only - last
The scheme is simple: add a table in the database: friends with the fields id, user_id, friend_id Then you just write down the right people here
- All I needed was, thanks a lot! Was it really that hard? - Kirpich643