Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Wyświetlenie kilku wyników z jednej tabeli dla jednego wyniku z innej
spit
post 15.07.2010, 19:45:56
Post #1





Grupa: Zarejestrowani
Postów: 135
Pomógł: 0
Dołączył: 1.08.2005

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


Trochę tytuł niewyraźny, ale nie wiem jak lepiej to opisać smile.gif

Piszę teraz taki wakacyjny projekcik, ogólnie chcę wyświetlić swoje zbiory starych gier na PC. Teoretycznie to jest do zrobienia w html w sekundę, ale chcę się pobawić php winksmiley.jpg Zamiast tłumaczenia pokażę moje zapytanie (naturalnie testowe, żeby ogarnąć kod, to nie jest żaden final):
  1. SELECT *
  2. FROM games
  3.  
  4. JOIN developers_games ON games_id = developers_games_gameid
  5. JOIN developers ON developers_games_devid = developers_id
  6.  
  7. JOIN genres_games ON games_id = genres_games_gameid
  8. JOIN genres ON genres_games_genreid = genres_id
  9.  
  10. JOIN publishers_games ON games_id = publishers_games_gameid
  11. JOIN publishers ON publishers_games_pubid = publishers_id
  12.  
  13. WHERE games_id = 1
  14. GROUP BY games_id

I mam problem. Z grupowaniem wyświetla się jeden wynik, ale ja mam przypisanych kilka gatunków. Na przykład dla gry "Road Rage" to będzie "akcja", "motory" i "wyścigi". Ale w tej chwili pokazuje mi się tylko jeden. Co zrobić, żeby pokazywały się wszystkie?

Wywołuję całość tak (oparte o ADOdb Lite):
  1. echo ' <ul>
  2. <li>Tytul: <a href="/games,id,'.$result->fields['games_id'].',n,'.$result->fields['games_slug'].'">'.$result->fields['games_name'].'</a></li>
  3. <li>Developer: <a href="/developers,id,'.$result->fields['developers_id'].'">'.$result->fields['developers_name'].'</a></li>
  4. <li>Wydawca: <a href="/publishers,id,'.$result->fields['publishers_id'].'">'.$result->fields['publishers_name'].'</a></li>
  5. <li>Gatunek: <a href="/genres,id,'.$result->fields['genres_id'].'">'.$result->fields['genres_name'].'</a></li>
  6. <li>Opis: '; ?><? if ($result->fields['games_desc'] == NULL) { echo 'Brak opisu'; } else { echo $result->fields['games_desc'].'</li>'; } ?>
  7. <? echo '
  8. </ul>';
Go to the top of the page
+Quote Post
wookieb
post 15.07.2010, 19:54:09
Post #2





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




Try
  1. GROUP BY games_id, genres_games_genreid


--------------------
Go to the top of the page
+Quote Post
spit
post 15.07.2010, 19:59:51
Post #3





Grupa: Zarejestrowani
Postów: 135
Pomógł: 0
Dołączył: 1.08.2005

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


Dzięki, ale teraz dublują się cała zawartość pętli (wywołuję to w while'u). Bez pętli jest tak, jak było.
Go to the top of the page
+Quote Post
wookieb
post 15.07.2010, 20:16:29
Post #4





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




A to widzisz źle skumałem
  1. SELECT * , GROUP_CONCAT(genres_name ORDER BY genres_name ASC) AS all_genres
  2. FROM games
  3.  
  4. JOIN developers_games ON games_id = developers_games_gameid
  5. JOIN developers ON developers_games_devid = developers_id
  6.  
  7. JOIN genres_games ON games_id = genres_games_gameid
  8. JOIN genres ON genres_games_genreid = genres_id
  9.  
  10. JOIN publishers_games ON games_id = publishers_games_gameid
  11. JOIN publishers ON publishers_games_pubid = publishers_id
  12.  
  13. WHERE games_id = 1
  14. GROUP BY games_id


Wynik masz w all_genres oddzielone przecinkiem


--------------------
Go to the top of the page
+Quote Post
spit
post 15.07.2010, 20:19:44
Post #5





Grupa: Zarejestrowani
Postów: 135
Pomógł: 0
Dołączył: 1.08.2005

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


Dzięki, prawie gra, ale wyświetla się podwójnie (w sensie gatunki) winksmiley.jpg Aha, i jak zrobić z tych wpisów (gatunków) linki? Probówałem
Kod
SELECT *, GROUP_CONCAT(' <a href=\"genres_id\">',genres_name,'</a>' ORDER BY genres_name ASC) as all_genres
ale wtedy nie bierze mi wartości, tylko drukuje samo genres_id.

Ten post edytował spit 15.07.2010, 20:25:31
Go to the top of the page
+Quote Post
wookieb
post 15.07.2010, 20:22:05
Post #6





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




  1. GROUP_CONCAT(DISTINCT genres_name ORDER BY genres_name ASC) AS all_genres


--------------------
Go to the top of the page
+Quote Post
spit
post 15.07.2010, 20:27:50
Post #7





Grupa: Zarejestrowani
Postów: 135
Pomógł: 0
Dołączył: 1.08.2005

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


Super, gra, dzięki wielkie smile.gif Jeszcze z tymi linkami, gdybyś mógł mnie poratować, byłbym megawdzięczny smile.gif
Go to the top of the page
+Quote Post
wookieb
post 15.07.2010, 20:36:14
Post #8





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




  1. GROUP_CONCAT(DISTINCT genres_name ORDER BY genres_name ASC) AS all_genres,
  2. GROUP_CONCAT(DISTINCT genres_id ORDER BY genres_name ASC) AS all_genres_ids


Masz listę idków oraz listę nazw grup. Rozbijasz sobie na tablicę obydwa pola
  1. $idki = explode(',', $record['all_genres_ids']);
  2. $grupy = explode(',' $record['all_genres']);
  3.  
  4. // laczymy w jedna tablice
  5. $lista_grup = array_combine($idki, $grupy);


no i teraz standardowo foreach na $lista_grup i masz wszystko co chcesz.


--------------------
Go to the top of the page
+Quote Post
spit
post 15.07.2010, 21:46:08
Post #9





Grupa: Zarejestrowani
Postów: 135
Pomógł: 0
Dołączył: 1.08.2005

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


Dzięki wielkie, gra smile.gif

Teraz zostaje mi problem z foreach, bo chcę z tego zrobić funkcję:
  1. function Links($arr) {
  2. foreach ($arr as $k => $v) {
  3. return '<a href="'.$k.'">'.$v.'</a>';
  4. }
  5. }
Ale zwraca tylko jedną wartość. Gdy dam 'echo' zamiast 'return', zwraca wszystko, ale tam gdzie jest funkcja, nie tam gdzie ją echuję.
Go to the top of the page
+Quote Post
wookieb
post 15.07.2010, 21:52:51
Post #10





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




Podstawy....
  1. function Links($arr) {
  2. $ret = '';
  3. foreach ($arr as $k => $v) {
  4. $ret.='<a href="'.$k.'">'.$v.'</a>';
  5. }
  6. return $ret;
  7. }
  8.  


Ten post edytował wookieb 15.07.2010, 21:53:23


--------------------
Go to the top of the page
+Quote Post
spit
post 15.07.2010, 21:56:26
Post #11





Grupa: Zarejestrowani
Postów: 135
Pomógł: 0
Dołączył: 1.08.2005

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


Heh, na to samo wpadłem, takie podstawy to zawsze największy problem smile.gif Niemniej dzięki wielkie za pomoc!
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: 8.07.2025 - 17:58