Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Szeregowanie alfabetyczne wg. danych z 2 tabeli
Forum PHP.pl > Forum > Bazy danych
L_Devil
Witam!

Mam następujący problem:

W mojej bazie danych mam dwie interesujące mnie tabele:

'users' o polach

id - int(7) PRIMARY, auto_increment
name - char(25)

i 'messages' o polach

owner - int(7)
from - int(7)
topic - char(50)
text - text


O co chodzi? Ano user po zalogowaniu dostaje porcję wiadomości o takim samym 'owner' jak jego 'id'. W miejsce pola 'from' dostaje 'name' z tabeli 'user' (czyli np. wiadomość dostał od kogoś o id 3 -> to wyświetli mu się że od pana YX).

Teraz w czym problem:

Chciałbym, aby użytkownik miał możliwość uszeregowania tych wiadomości, gdyż może ich być dość sporo w systemie. Po pierwsze można szeregować alfabetycznie przez podanie "ORDER by 'topic' ASC". Oki. Ale ja teraz chciałbym zrobić jeszcze szeregowanie wg. alfabetycznie osób, które przysłały nam wiadomość. I tutaj pojawia się mój problem:
jeżeli wpiszę "ORDER by 'from' " to dostanę poszeregowane wg. id, nie wg. imienia. Rozumiem że trzeba wykonać zapytania do obu tabel. Ale jak?
vieri_pl
Jeśli chodzi o sortowanie ja to rozwiązałem tak:

  1. <?php
  2.  
  3. if($uart_sort=='') $sort = $art_sort; else $sort = $uart_sort;
  4.  
  5.  
  6. echo '<br><div align="center" class="uni_01">sortuj według: <b>tytułu</b> <a title="alfabetycznie" href="strona.php?sortby=pole-ASC"><img src="images/s_down.gif" border="0" align="absmiddle"></a><a
  7. href="strona.php?sortby=pole-DESC"><img src="images/s_up.gif" border="0" align="absmiddle"></a>';
  8. ?>


U góry wyświetlamy linki, a teraz pobieramy...

  1. $query = "SELECT *
  2. FROM tabela ORDER BY $sort";
  3. $result = mysql_query($query);



Pisane z ręki może być multum bledów smile.gif
kszychu
  1. SELECT messages.*, users.name AS name
  2. FROM users, messages
  3. WHERE users.id = messages.
  4. FROM <span style='color:blue'>AND messages.owner = '5'
  5. ORDER BY name


To '5' to przykładowe id zalogowanego użytkownika.
L_Devil
Wielkie dzięki Krzychu, działa prawie tak jak miało działać... 'prawie'. No właśnie, jest tutaj problem o którym nie wspomniałem (przyznaję, mój błąd). Niektóre rekordy wskazują 'ślepo' (tzn. za nadawcę jest podstawiona wartość '0') - niestety w takiej sytuacji twoje zapytanie zingoruje te rekordy, a powinno je również wyświetlić na końcu/początku listy (w zależności czy pojawia się słówko "asc" czy "desc", mnie nie robi większej różnicy które do którego winksmiley.jpg )
DeyV
Kod
SELECT messages.*, users.name as name
FROM messages
LEFT JOIN users ON users.id = messages.from
WHERE messages.owner = '5'
ORDER BY name
kamilmm
Problem z szeregowaniem według nazw


Witam
mam taki skrypt do galerii zdjęć:
  1. <?
  2. header("Cache-Control: no-store, no-cache, must-revalidate");
  3. header("Pragma: no-cache");
  4.  
  5. require('./sp_conf.php');
  6.  
  7. include $gheader;
  8. $header_html = str_replace('{NAME}', $gallery_name, $header_html);
  9. $header_html = str_replace('{DOWNLINK}', 'http://www.adresstrony.pl/', $header_html);
  10. $header_html = str_replace('{AUTHORLINK}', 'http://www.adresstrony.pl/', $header_html);
  11. $header_html = str_replace('{BACKLINK}', $back_link, $header_html);
  12. $header_html = str_replace('{CLOSELINK}', $close_link, $header_html);
  13. $header_html = str_replace('{COPYRIGHT}', $copyright, $header_html);
  14. $header_html = str_replace('{HOMELINK}', $home_link, $header_html);
  15. echo $header_html;
  16.  
  17. $dp = @opendir($directory);
  18. if ( !$dp )
  19. {
  20. echo "Opening directory " . $directory . " <b>failed</b>. Incorrect path?";
  21. echo "n<br />I cannot go on any longer. I'm sorry. I tried.n<br />";
  22. }
  23. else
  24. {
  25. $extensions = array(
  26. 'jpg',
  27. 'jpeg',
  28. 'gif',
  29. 'png',
  30. 'bmp',
  31. 'tiff',
  32. 'tif',
  33. 'psd'
  34. );
  35.  
  36. require($glayout);
  37.  
  38. $i=1;
  39. $x=1;
  40.  
  41. while ( $filename = readdir($dp) )
  42. {
  43. $theline = $line;
  44. $fileparts = explode(".", strrev($filename), 2);
  45. $name = strrev($fileparts[1]);
  46. $ext = strrev($fileparts[0]);
  47. unset($fileparts);
  48. if ( in_array(strtolower($ext), $extensions) )
  49. {
  50. if ( $x == 1 )
  51. {
  52. echo $line_start;
  53. }
  54. $image_url = './sp_images.php?size=' . $max_size . '&filename=' . $filename . '&action=thumb';
  55. $theline = str_replace('{IMAGE_URL}', $image_url, $theline);
  56. if ( $spoofing )
  57. {
  58. $link = './sp_images.php?filename=' . $filename . '&action=spoof';
  59. }
  60. else
  61. {
  62. $link = $images_uri . $filename;
  63. }
  64. $num = $x;
  65.  
  66. $theline = str_replace('{ROWPOS}', $num, $theline);
  67. $theline = str_replace('{NUMBER}', $i, $theline);
  68. $theline = str_replace('{LINK}', $link, $theline);
  69. $name = str_replace("%20", " ", $name);
  70. $name = str_replace("_", " ", $name);
  71. $theline = str_replace('{NAME}', $name, $theline);
  72. $theline = str_replace('{FILESIZE}', (int)(filesize($directory . $filename)/1024), $theline);
  73. $theline = str_replace('{FILENAME}', $filename, $theline);
  74. $theline = str_replace('{DOWNLINK}', 'http://www.adresstrony.pl/', $theline);
  75. $theline = str_replace('{AUTHORLINK}', 'http://www.adresstrony.pl/', $theline);
  76. echo $theline;
  77.  
  78. if ( $x == (int)$per_row )
  79. {
  80. echo $line_end;
  81. $x=1;
  82. }
  83. else
  84. {
  85. $x++;
  86. }
  87. $i++;
  88. }
  89. }
  90. $terminate_leftover = str_replace('{DOWNLINK}', 'http://www.adresstrony.pl/', $terminate_leftover);
  91. $terminate_leftover = str_replace('{AUTHORLINK}', 'http://www.adresstrony.pl/', $terminate_leftover);
  92. $terminate_leftover = str_replace('{NUMLEFT}', (($per_row-$x)+1), $terminate_leftover);
  93. echo $terminate_leftover;
  94. }
  95.  
  96. include $gfooter;
  97. $footer_html = str_replace('{NAME}', $gallery_name, $footer_html);
  98. $footer_html = str_replace('{DOWNLINK}', 'http://www.adresstrony.pl/', $footer_html);
  99. $footer_html = str_replace('{AUTHORLINK}', 'http://www.adresstrony.pl/', $footer_html);
  100. $footer_html = str_replace('{BACKLINK}', $back_link, $footer_html);
  101. $footer_html = str_replace('{CLOSELINK}', $close_link, $footer_html);
  102. $footer_html = str_replace('{COPYRIGHT}', $copyright, $footer_html);
  103. $footer_html = str_replace('{HOMELINK}', $home_link, $footer_html);
  104. echo $footer_html;
  105.  
  106. ?>


co zrobić zeby zdjęcia ustawiały mi się według nazw questionmark.gif?

001.jpg
002.jpg
itd a nie losowo,
prosze o pomoc
erix
Wciśnij nazwy plików do tablicy i potraktuj natsort" title="Zobacz w manualu php" target="_manualem.
kamilmm
erix,

nie wychodzi mi to ciagle jest blad albo nic sie nie wyswietla sadsmiley02.gif
moglbys mi pomoc?questionmark.gif?
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.