Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php]Grupowanie wyników
Forum PHP.pl > Forum > Przedszkole
seba15290
Witam mam taki skrypt:
  1. <?php
  2. include ('./mysql_connect.php');
  3. $zapytanie = "select * from kraj as k, people as p, adresy as a where p.id_people=a.id_pe
    ople and k.id_kraj=a.id_kraj order by p.nazwisko, p.imie;"
    ;
  4.  
  5.  
  6. if (!mysql_query($zapytanie))
  7. print "Nie udało sie odczytac danych z tabeli"; 
  8.  
  9. $wynik = mysql_query($zapytanie)
  10. or die('Błąd zapytania'); 
  11.  
  12. $ile_znalezionych=mysql_num_rows($wynik);
  13. echo "Biogramy ($ile_znalezionych)";
  14.  
  15. echo "<table cellpadding=\"0\" border=0 align=\"center\">";
  16.  echo "<tr>";
  17. echo "<td>".imie,' ',i,' ',nazwisko."</td>";
  18. echo "</tr>";
  19. while($r = mysql_fetch_array($wynik)) {
  20.  
  21. $cat=$r['id_people'];
  22.  
  23.  echo "<tr>";
  24. echo "<td><a href=\"biogram.php?id_cat={$cat}\">".$r['imie'],' ', $r['nazwisko']."</a></td>";
  25. echo "</tr>";
  26.  
  27.  
  28. }
  29. echo "</table>";
  30.  
  31.  
  32.  
  33.  
  34. ?>

Teraz wyświetla mi osoby alfabetycznie, ale ja chciałbym aby wyniki wyświetlały się tak jak na tej stronie.
nospor
  1. <?php
  2. $data = array('Adam','ala','Bartek','Celinka','Cwikla','Inne do bolu');
  3. $lastLetter = null;
  4. foreach ($data as $name){
  5. $firstLetter = strtoupper(substr($name,0,1));
  6. if ($firstLetter != $lastLetter){
  7. $lastLetter = $firstLetter;
  8. echo "<b>$firstLetter</b><br />";
  9. }
  10. echo $name.'<br />';
  11. }
  12. ?>

Podczep to do siebie.

proszę poprawić tytuł o znacznik zgodnie z zasadami forum Przedszkole:
Temat: Tematyka i zasady panujace na forum Przedszkole
seba15290
Cytat(nospor @ 19.10.2007, 12:42:57 ) *
  1. <?php
  2. $data = array('Adam','ala','Bartek','Celinka','Cwikla','Inne do bolu');
  3. $lastLetter = null;
  4. foreach ($data as $name){
  5. $firstLetter = strtoupper(substr($name,0,1));
  6. if ($firstLetter != $lastLetter){
  7. $lastLetter = $firstLetter;
  8. echo "<b>$firstLetter</b><br />";
  9. }
  10. echo $name.'<br />';
  11. }
  12. ?>

Podczep to do siebie.

proszę poprawić tytuł o znacznik zgodnie z zasadami forum Przedszkole:
Temat: Tematyka i zasady panujace na forum Przedszkole


  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3.  
  4. <head>
  5. <title>Wyniki wyszukiwania</title>
  6. <meta name="GENERATOR" content="Quanta Plus">
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  8. </head>
  9. <body>
  10. <?php
  11. include ('./mysql_connect.php');
  12. $zapytanie = "select * from kraj as k, people as p, adresy as a where p.id_people=a.id_pe
    ople and k.id_kraj=a.id_kraj order by p.nazwisko, p.imie;"
    ;
  13.  
  14.  
  15. if (!mysql_query($zapytanie))
  16. print "Nie udało sie odczytac danych z tabeli"; 
  17.  
  18. $wynik = mysql_query($zapytanie)
  19. or die('Błąd zapytania'); 
  20.  
  21. $ile_znalezionych=mysql_num_rows($wynik);
  22. echo "Biogramy ($ile_znalezionych)";
  23.  
  24. echo "<table cellpadding=\"0\" border=0 align=\"center\">";
  25.  echo "<tr>";
  26. //echo "<td>".imie,' ',i,' ',nazwisko."</td>";
  27. echo "</tr>";
  28.  
  29. while($r = mysql_fetch_array($wynik)) {
  30.  
  31. $cat=$r['id_people'];
  32. $nazwisko=$r['nazwisko'];
  33.  
  34.  
  35. $data = array($nazwisko);
  36. $lastLetter = null;
  37. foreach ($data as $name){
  38. $firstLetter = strtoupper(substr($name,0,1));
  39. if ($firstLetter != $lastLetter){
  40. $lastLetter = $firstLetter;
  41. echo "<td><b>$firstLetter</b></td><br />";
  42.  }
  43. echo "<tr>";
  44. echo "<td><a href=\"biogram.php?id_cat={$cat}\">".$name, ' ', $r['imie']."</a></td><br />";
  45. echo "</tr>";
  46.  
  47. }
  48. }
  49.  
  50.  
  51.  echo "</table>";
  52.  
  53.  
  54.  
  55.  
  56. ?>
  57.  
  58. </body>
  59. </html>

Mam dwie osoby których nazwiska zaczynają się na M i dla każdej wyświetla się litera M.Jak zrobić aby każda litera wyświetlała się tylko raz.
nospor
zupelnie źle podpiales mechanizm ktory ci podalem. Pomysl troche. Moja petla to twoja glowna petla dla zapytania. a nie ty jeszcze wewnatrz tworzysz sztuczną petle...
seba15290
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3.  
  4. <head>
  5. <title>Wyniki wyszukiwania</title>
  6. <meta name="GENERATOR" content="Quanta Plus">
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  8. <style type="text/css" media="screen">@import "includes/layout.css";</style>
  9. </head>
  10. <body>
  11. <?php
  12. require_once ('./mysql_connect.php');
  13. $zapytanie = "select * from kraj as k, people as p, adresy as a where p.id_people=a.id_pe
    ople and k.id_kraj=a.id_kraj order by p.nazwisko, p.imie;"
    ;
  14.  
  15.  
  16. if (!mysql_query($zapytanie))
  17. print "Nie udało sie odczytac danych z tabeli"; 
  18.  
  19. $wynik = mysql_query($zapytanie)
  20. or die('Błąd zapytania'); 
  21.  
  22. $ile_znalezionych=mysql_num_rows($wynik);
  23. echo "Biogramy ($ile_znalezionych)<br />";
  24.  // echo "<table cellpadding=\"0\" border=0 align=\"center\">";
  25.  
  26.  
  27. $r = mysql_fetch_array($wynik);
  28. $nazwisko=$r['nazwisko'];
  29. $data = array($nazwisko);
  30. $lastLetter = null;
  31. foreach ($data as $name){
  32. $firstLetter = strtoupper(substr($name,0,1));
  33. if ($firstLetter != $lastLetter){
  34. $lastLetter = $firstLetter;
  35. echo "<b>$firstLetter</b><br />";
  36. }
  37. echo $name.'<br />';
  38. }
  39.  
  40. ?>
  41.  
  42. </body>
  43. </html>

Wstawiłem to tak i teraz wyświetla mi ostatni rekord z pola nazwisko, jak zrobić aby wyświetlał wszystkie nazwiska?

Cytat(nospor @ 21.10.2007, 21:54:14 ) *
zupelnie źle podpiales mechanizm ktory ci podalem. Pomysl troche. Moja petla to twoja glowna petla dla zapytania. a nie ty jeszcze wewnatrz tworzysz sztuczną petle...


Nie bardzo rozumiem jak mam więc podpiąć ten mechanizm. Dopiero zaczynam uczyć się php i jetem początkujący. Rozumiem że w tej linijce:
  1. <?php
  2. $data = array('Adam','ala','Bartek','Celinka','Cwikla','Inne do bolu');
  3. ?>
mam wstwić wyniki zapytania więc zastosowałem mysql_fetch_array. Gdybyś mógł pomóż mito podpiąć bo mi jest to potrzebne na wtorek. Zgóry dzięki
bhacas
To Twoja pętla while i foreach, resztę sobie dopisz.
  1. <?php
  2. while($r = mysql_fetch_array($wynik)) {
  3.  
  4. $nazwisko[]=$r['nazwisko'];
  5. }
  6.  
  7. $lastLetter = null;
  8. foreach ($nazwisko as $name){
  9. $firstLetter = strtoupper(substr($name,0,1));
  10. if ($firstLetter != $lastLetter){
  11. $lastLetter = $firstLetter;
  12. echo "<td><b>$firstLetter</b></td><br />";
  13.  }
  14. echo "<tr>";
  15. echo "<td><a href=\"biogram.php?id_cat={$cat}\">".$name, ' ', $r['imie']."</a></td><br />";
  16. echo "</tr>";
  17.  
  18. }
  19. ?>
seba15290
Cytat(bhacas @ 2.11.2007, 20:22:03 ) *
To Twoja pętla while i foreach, resztę sobie dopisz.
  1. <?php
  2. while($r = mysql_fetch_array($wynik)) {
  3.  
  4. $nazwisko[]=$r['nazwisko'];
  5. }
  6.  
  7. $lastLetter = null;
  8. foreach ($nazwisko as $name){
  9. $firstLetter = strtoupper(substr($name,0,1));
  10. if ($firstLetter != $lastLetter){
  11. $lastLetter = $firstLetter;
  12. echo "<td><b>$firstLetter</b></td><br />";
  13.  }
  14. echo "<tr>";
  15. echo "<td><a href=\"biogram.php?id_cat={$cat}\">".$name, ' ', $r['imie']."</a></td><br />";
  16. echo "</tr>";
  17.  
  18. }
  19. ?>

Wielkie dzięki, działa guitar.gif


Cytat(bhacas @ 2.11.2007, 20:22:03 ) *
To Twoja pętla while i foreach, resztę sobie dopisz.
  1. <?php
  2. while($r = mysql_fetch_array($wynik)) {
  3.  
  4. $nazwisko[]=$r['nazwisko'];
  5. }
  6.  
  7. $lastLetter = null;
  8. foreach ($nazwisko as $name){
  9. $firstLetter = strtoupper(substr($name,0,1));
  10. if ($firstLetter != $lastLetter){
  11. $lastLetter = $firstLetter;
  12. echo "<td><b>$firstLetter</b></td><br />";
  13.  }
  14. echo "<tr>";
  15. echo "<td><a href=\"biogram.php?id_cat={$cat}\">".$name, ' ', $r['imie']."</a></td><br />";
  16. echo "</tr>";
  17.  
  18. }
  19. ?>

Jeszcze mam takie pytanie, jak zrobić abym miał jeszcze inne pola takie jak imie i id_people?
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.