Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: kolejność "id" oraz enter msql
Forum PHP.pl > Forum > PHP
Randallmaster
Mam 2 problemy na swojej stronie www... Znalazłem kod i przerobiłem do własnych potrzeb. Jest to kod paginacji połączony z wyświetlaniem rekordów na stronie. Męczę się od dłuższego czasu żeby zmienić kolejność aby było:

40
39
38
37

a jest: sad.gif

1
2
3
4

oto kod:
  1. <?php
  2.  
  3.  
  4. $tbl_name="damprace"; //your table name
  5. // How many adjacent pages should be shown on each side?
  6. $adjacents = 3;
  7.  
  8. /*
  9. First get total number of rows in data table.
  10. If you have a WHERE clause in your query, make sure you mirror it here.
  11. */
  12. $query = "SELECT COUNT(*) as num FROM $tbl_name";
  13. $total_pages = mysql_fetch_array(mysql_query($query));
  14. $total_pages = $total_pages[num];
  15.  
  16. /* Setup vars for query. */
  17. $targetpage = "index.php"; //your file name (the name of this file)
  18. $limit = 15; //how many items to show per page
  19. $page = $_GET['page'];
  20. if($page)
  21. $start = ($page - 1) * $limit; //first item to display on this page
  22. else
  23. $start = 0; //if no page var is given, set start to 0
  24.  
  25. /* Get data. */
  26. $sql = "SELECT * FROM $tbl_name LIMIT $start, $limit";
  27. $result = mysql_query($sql);
  28.  
  29. /* Setup page vars for display. */
  30. if ($page == 0) $page = 1; //if no page var is given, default to 1.
  31. $prev = $page - 1; //previous page is page - 1
  32. $next = $page + 1; //next page is page + 1
  33. $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
  34. $lpm1 = $lastpage - 1; //last page minus 1
  35.  
  36. /*
  37. Now we apply our rules and draw the pagination object.
  38. We're actually saving the code to a variable in case we want to draw it more than once.
  39. */
  40. $pagination = "";
  41. if($lastpage > 1)
  42. {
  43. $pagination .= "<div class=\"pagination\">";
  44. //previous button
  45. if ($page > 1)
  46. $pagination.= "<a href=\"$targetpage?page=$prev\">poprzednia</a>";
  47. else
  48. $pagination.= "<span class=\"disabled\">poprzednia</span>";
  49.  
  50. //pages
  51. if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
  52. {
  53. for ($counter = 1; $counter <= $lastpage; $counter++)
  54. {
  55. if ($counter == $page)
  56. $pagination.= "<span class=\"current\">$counter</span>";
  57. else
  58. $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
  59. }
  60. }
  61. elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
  62. {
  63. //close to beginning; only hide later pages
  64. if($page < 1 + ($adjacents * 2))
  65. {
  66. for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
  67. {
  68. if ($counter == $page)
  69. $pagination.= "<span class=\"current\">$counter</span>";
  70. else
  71. $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
  72. }
  73. $pagination.= "...";
  74. $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
  75. $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
  76. }
  77. //in middle; hide some front and some back
  78. elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
  79. {
  80. $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
  81. $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
  82. $pagination.= "...";
  83. for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
  84. {
  85. if ($counter == $page)
  86. $pagination.= "<span class=\"current\">$counter</span>";
  87. else
  88. $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
  89. }
  90. $pagination.= "...";
  91. $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
  92. $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
  93. }
  94. //close to end; only hide early pages
  95. else
  96. {
  97. $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
  98. $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
  99. $pagination.= "...";
  100. for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
  101. {
  102. if ($counter == $page)
  103. $pagination.= "<span class=\"current\">$counter</span>";
  104. else
  105. $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
  106. }
  107. }
  108. }
  109.  
  110. //next button
  111. if ($page < $counter - 1)
  112. $pagination.= "<a href=\"$targetpage?page=$next\">następna</a>";
  113. else
  114. $pagination.= "<span class=\"disabled\">następna</span>";
  115. $pagination.= "</div>\n";
  116. }
  117. ?>
  118.  
  119. <?php
  120.  
  121. // Define $color=1
  122. $color="1";
  123.  
  124. echo '<table width="800" border="0" cellpadding="3" cellspacing="0">';
  125. echo "<tr>
  126. <td width='30px'><font color='#a4a4a4' size='1'>ID</td>
  127. <td width='120px'><font color='#a4a4a4' size='1'>DATA</td>
  128. <td width='250px'><font color='#a4a4a4' size='1'>STANOWISKO</td>
  129. <td width='250px'><font color='#a4a4a4' size='1'>FIRMA</td>
  130. <td width='150px'><font color='#a4a4a4' size='1'>LOKALIZACJA</td></tr>";
  131. while($row=mysql_fetch_array($result)){
  132.  
  133. // If $color==1 table row color = #FFC600
  134. if($color==1){
  135.  
  136.  
  137. echo "<tr bgcolor='#b0bccd'>
  138. <td width='30px'>".$row['id']."</td>
  139. <td width='120px'>".$row['data']."</td>
  140. <td width='250px'><a href='http://pracujkalisz.pl/test/stanowisko.php?id=".$row['id']."'>".$row['stanowisko']."</a></td>
  141. <td width='250px'>".$row['firma']."</td>
  142. <td width='150px'>".$row['lokalizacja']."</td>
  143. </tr>";
  144. // Set $color==2, for switching to other color
  145. $color="2";
  146. }
  147.  
  148. // When $color not equal 1, use this table row color
  149. else {
  150. echo "<tr bgcolor='#dde3ec'>
  151. <td width='30px'>".$row['id']."</td>
  152. <td width='120px'>".$row['data']."</td>
  153. <td width='250px'><a href='http://pracujkalisz.pl/test/stanowisko.php?id=".$row['id']."'>".$row['stanowisko']."</a></td>
  154. <td width='250px'>".$row['firma']."</td>
  155. <td width='150px'>".$row['lokalizacja']."</td>
  156. </tr>";
  157. // Set $color back to 1
  158. $color="1";
  159. }
  160.  
  161. }
  162. echo '</table>';
  163.  
  164. ?>
  165. <p>
  166. <?=$pagination?>


oraz mam problem z wyświetlaniem z bazy danych entera.... Zamiast pokazywania się enter robi się spacja sad.gif jaki kod jest za to odpowiedzialny?? dodam że wszystkie znaki żźćśąę działają poprawnie.

Z góry dziękuję za pomoc,
Randall
nospor
Cytat
Zamiast pokazywania się enter robi się spacja
HTML nie uznaje entera. W HTML nową linią jest <br />
Użyj funkcji nl2br() by zmienić enter na br
thek
Aby kod Ci działał ja chcesz, musisz pętlę zrobić "na odwrót". W tej chwili zapytanie wyszukuje Ci rekordy po kolei od pierwszego. Ustaw więc ORDER BY na idący nie rosnąco, ale malejąco. A potem to już tylko kwestia modyfikacji pętli, by nie pokazywało jako liczbę 1,2,3 ale max_page, max_page-1 itd. To jedyne miejsca jakie tak naprawdę są konieczne do zmiany. Reszta kodu w zasadzie może być nie tknięta palcem.
Randallmaster
Hmmmm dziękuję nospor nl2br() zadziałało smile.gif piwko dla ciebie...
Natomiast ciągle nie wiem co ze zmianą id ;( bo nie wiem gdzie jest ORDER BY...
nospor
Cytat
( bo nie wiem gdzie jest ORDER BY...
w zapytaniu.
http://dev.mysql.com/doc/refman/5.0/en/select.html
Randallmaster
Dziękuje smile.gif już zrozumiałem:

  1. $sql = "SELECT * FROM $tbl_name ORDER by id DESC LIMIT $start, $limit";


wystarczyło dodać ORDER BY id i sortowanie DESC lub ASC smile.gif dziękuję serdecznie za pomoc smile.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.