Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] [MySQL] wyświetlenie wyników + sortowanie
Forum PHP.pl > Forum > Przedszkole
tobiaszf
Jak wyświetlić wyniki z bazy, żeby były widoczne również nagłówki, tak żeby potem po nagłówkach można było sortować na stronie (tzn. jak klikam na hiperłącze w nagłówku to cała tabela jest sortowana właśnie po tym nagłówku)


strona gdzie jest skrypt

  1. <?php
  2.  
  3. $connection = @mysql_connect('formularz.fo.funpicsql.de', '***', '****')
  4. or die('error');
  5. $db = @mysql_select_db('****', $connection)
  6. or die('error');
  7.  
  8.  
  9.  
  10. $result = mysql_query("SELECT * FROM test ORDER BY distance")
  11. or die('error');
  12.  
  13.  
  14.  
  15. header('Content-type: text/html; charset=utf-8');
  16. if(mysql_num_rows($result) > 0) {
  17.  
  18. echo "<table cellpadding=\"1\" border=1>";
  19. while($r = mysql_fetch_assoc($result)) {
  20. echo "<tr>";
  21. echo "<TR>";
  22. echo "</TR>";
  23. echo "<td>".$r['name']."</td>";
  24. echo "<td>".$r['surrname']."</td>";
  25. echo "<td>".$r['distance']."</td>";
  26.  
  27. echo "<td>
  28. <a href=\"results.php?a=del&amp;id={$r['id']}\">DEL</a>
  29. <a href=\"results.php?a=edit&amp;id={$r['id']}\">ADD room</a>
  30. </td>";
  31. echo "</tr>";
  32. }
  33. echo "</table>";
  34. }
  35. $a = trim($_GET['a']);
  36. $id = trim($_GET['id']);
  37.  
  38. if($a == 'del' and !empty($id)) {
  39.  
  40.  
  41. mysql_query("DELETE FROM test WHERE id='$id'")
  42. or die('error '.mysql_error());
  43.  
  44. echo 'ok';
  45. }
  46.  
  47. $a = trim($_REQUEST['a']);
  48. $id = trim($_GET['id']);
  49.  
  50. if($a == 'edit' and !empty($id)) {
  51.  
  52. $result = mysql_query("SELECT * FROM test WHERE
  53. id='$id'")
  54. or die('error');
  55.  
  56. if(mysql_num_rows($result) > 0) {
  57.  
  58. $r = mysql_fetch_assoc($result);
  59.  
  60. echo '<form action="results.php" method="post">
  61. <input type="hidden" name="a" value="save" />
  62. <input type="hidden" name="id" value="'.$id.'" />
  63. room:<br />
  64. <input type="text" name="room"
  65. value="'.$r['room'].'" /><br />
  66. <input type="submit" value="save" />
  67. </form>';
  68. }
  69. }
  70. elseif($a == 'save') {
  71.  
  72. $id = $_POST['id'];
  73. $room = trim($_POST['room']);
  74.  
  75. mysql_query("UPDATE test SET room='$room',
  76. room='$room' WHERE id='$id'")
  77. or die('error');
  78. echo 'ok';
  79. }
  80.  
  81.  
  82. ?>
krzywy36
Nagłówki z reguły sam ustalasz. A sortowanie to po prostu dopisanie jakiejś zmiennej do adresu po kliknięciu w nagłówek a potem w zależności od tej zmiennej dopisujesz do zapytania 'ORDER BY pole ASC/DESC'.
tobiaszf
Doszedłem do czegoś takiego: Nie wiem jednak co zrobić żeby można było segregać po nagłówkach


  1. <?php
  2.  
  3. $connection = @mysql_connect('*********', '***', '******')
  4. or die('error');
  5. $db = @mysql_select_db('******', $connection)
  6. or die('error');
  7.  
  8.  
  9.  
  10. $result = mysql_query("SELECT * FROM test ORDER BY room")
  11. or die('error');
  12.  
  13.  
  14.  
  15. header('Content-type: text/html; charset=utf-8');
  16. if(mysql_num_rows($result) > 0) {
  17.  
  18.  
  19.  
  20. echo "<table cellpadding=\"1\" border=1>";
  21. echo "<tr>
  22. <td><a href='".$_SERVER['PHP_SELF']."?s=1'>Last</a></td>\n
  23. <td><a href='".$_SERVER['PHP_SELF']."?s=2'>First</a></td>\n
  24. <td><a href='".$_SERVER['PHP_SELF']."?s=3'>Room</a></td>\n
  25. </tr>";
  26. while($r = mysql_fetch_assoc($result)) {
  27. if($_GET['1']){
  28.  
  29. $result = mysql_query("SELECT * FROM test ORDER BY name")
  30. or die('error');
  31.  
  32. }
  33.  
  34. echo "<tr>";
  35. echo "<TR>";
  36. echo "</TR>";
  37. echo "<td>".$r['name']."</td>";
  38. echo "<td>".$r['surrname']."</td>";
  39. echo "<td>".$r['distance']."</td>";
  40.  
  41.  
  42. </td>";
  43. echo "</tr>";
  44. }
  45. echo "</table>";
  46. }
  47.  
  48.  
  49. ?>
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.