Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Znajomi na stronie :) mysql, pdo
seba199696
post 13.12.2011, 22:57:00
Post #1





Grupa: Zarejestrowani
Postów: 119
Pomógł: 0
Dołączył: 4.01.2011

Ostrzeżenie: (20%)
X----


Kto mi wytłumaczy jak zrobić znajomych w mysqlu? proszę o pomoc smile.gif
Go to the top of the page
+Quote Post
mis
post 13.12.2011, 23:02:54
Post #2





Grupa: Zarejestrowani
Postów: 71
Pomógł: 2
Dołączył: 25.04.2010

Ostrzeżenie: (0%)
-----


jeśli dobrze rozumiem, to najłatwiej możesz po prostu zrobić w tabeli z userami dodać nowa kolumne `friends` i tam wrzucać po przecinku id inncyh ludzi, a potem przy wyświetlaniu robisz tablice - explode i heja.
Go to the top of the page
+Quote Post
seba199696
post 13.12.2011, 23:11:46
Post #3





Grupa: Zarejestrowani
Postów: 119
Pomógł: 0
Dołączył: 4.01.2011

Ostrzeżenie: (20%)
X----


ee nie wiem czemu ale wydaje mi się że głupi pomysł troszq

Ten post edytował seba199696 13.12.2011, 23:13:23
Go to the top of the page
+Quote Post
rafal.t
post 13.12.2011, 23:29:13
Post #4





Grupa: Zarejestrowani
Postów: 13
Pomógł: 6
Dołączył: 2.01.2010

Ostrzeżenie: (0%)
-----


Możesz utworzyć oddzielną tabele gdzie będziesz miał w jednej kolumnie id_user-a a w drugiej id_user-a, który jest przyjacielem. Taka tabelka users2friends.


--------------------
---------------------------------------------------------
https://twitter.com/#!/RafalTokar
Go to the top of the page
+Quote Post
seba199696
post 13.12.2011, 23:34:02
Post #5





Grupa: Zarejestrowani
Postów: 119
Pomógł: 0
Dołączył: 4.01.2011

Ostrzeżenie: (20%)
X----


powiedzmy coś takiego tak?

friends
id | user1 | user2

czyli jak będę wyszukiwał znajomych usera o id 1 to będzie:

  1. SELECT * FROM tabela WHERE `user1` = '1' OR `user2` = '1'
?

Ten post edytował seba199696 13.12.2011, 23:38:46
Go to the top of the page
+Quote Post
IceManSpy
post 13.12.2011, 23:35:54
Post #6





Grupa: Zarejestrowani
Postów: 1 006
Pomógł: 111
Dołączył: 23.07.2010
Skąd: Kraków

Ostrzeżenie: (0%)
-----


  1. SELECT * FROM tabela WHERE `user1` = '1' OR `user2` = '1'

jeśli chodzi o poprawność zapytania wink.gif

Ale coś o taki deseń chodzi.


--------------------
Go to the top of the page
+Quote Post
seba199696
post 13.12.2011, 23:37:49
Post #7





Grupa: Zarejestrowani
Postów: 119
Pomógł: 0
Dołączył: 4.01.2011

Ostrzeżenie: (20%)
X----


a w jaki sposób mam odczytać nazwę użytkownika?
Go to the top of the page
+Quote Post
rafal.t
post 13.12.2011, 23:44:32
Post #8





Grupa: Zarejestrowani
Postów: 13
Pomógł: 6
Dołączył: 2.01.2010

Ostrzeżenie: (0%)
-----


Jeśli szukasz znajmoych dla usera o id = 1 to wystarczy taki sql:

  1. SELECT * FROM tabela WHERE user1 = 1

lub
  1. SELECT user2 FROM tabela WHERE user1 = 1


Oczywiście warto gdzieś w kodzie uniemożliwić dodawanie siebie do swoich znajomych wink.gif

Poczytaj o JOIN w sql i połącz dwie tabele: dane z tabeli z id przyjaciół z ich danymi z tabeli user


--------------------
---------------------------------------------------------
https://twitter.com/#!/RafalTokar
Go to the top of the page
+Quote Post
seba199696
post 14.12.2011, 18:16:18
Post #9





Grupa: Zarejestrowani
Postów: 119
Pomógł: 0
Dołączył: 4.01.2011

Ostrzeżenie: (20%)
X----


Okej smile.gif Mniej więcej już wiem o co chodzi

zrobiłem sobie 2 tabele

users
id_user | name

friends
user1 | user2

I chcę wyświetlić nazwy użytkowników znajomych od 1 (id_user = 1) czyli:

  1. SELECT * FROM users
  2. JOIN friends ON
  3. (users.id_user = friends.user1 OR users.id_user = friends.user2)
  4. WHERE friends.user1 = 1 OR friends.user2 = 1
Tak?

Jeśli to jest dobrze to jak mam zrobić żeby nie wyświetlało mojej nazwy użytkownika w znajomych?
Go to the top of the page
+Quote Post
rafal.t
post 14.12.2011, 20:53:00
Post #10





Grupa: Zarejestrowani
Postów: 13
Pomógł: 6
Dołączył: 2.01.2010

Ostrzeżenie: (0%)
-----


Dla usera o id 1, spróbuj coś takiego:

  1. SELECT * FROM users LEFT JOIN friends ON users.id_user = friends.user2 WHERE friends.user1 = 1


--------------------
---------------------------------------------------------
https://twitter.com/#!/RafalTokar
Go to the top of the page
+Quote Post
seba199696
post 14.12.2011, 21:00:48
Post #11





Grupa: Zarejestrowani
Postów: 119
Pomógł: 0
Dołączył: 4.01.2011

Ostrzeżenie: (20%)
X----


no ale jak mam smile.gif np.

user1 | user2
1 | 2
1 | 3
3 | 1

i jak chce pokazać znajomych od usera o id: 1

to mi pokaże: 2/3 ale już 3 nie pokaże rozumiesz o co mi chodzi?

Ten post edytował seba199696 14.12.2011, 21:01:28
Go to the top of the page
+Quote Post
rafal.t
post 14.12.2011, 21:09:54
Post #12





Grupa: Zarejestrowani
Postów: 13
Pomógł: 6
Dołączył: 2.01.2010

Ostrzeżenie: (0%)
-----


Spróbuj pokombinować z czymś takim:

  1. SELECT * FROM users
  2. JOIN friends ON
  3. (users.id_user = friends.user1 OR users.id_user = friends.user2)
  4. WHERE (friends.user1 = 1 OR friends.user2 = 1) AND users.id != 1


--------------------
---------------------------------------------------------
https://twitter.com/#!/RafalTokar
Go to the top of the page
+Quote Post
seba199696
post 14.12.2011, 21:59:55
Post #13





Grupa: Zarejestrowani
Postów: 119
Pomógł: 0
Dołączył: 4.01.2011

Ostrzeżenie: (20%)
X----


hah biggrin.gif Działa smile.gif Dziękuje!
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 15.07.2025 - 19:54