Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Paginacja i 10 rekordów
Forum PHP.pl > Forum > PHP
wlodek_789
Mam paginację
  1. Ť Previous 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Next ť
ale nie wiem jak zrobić limit na np 10
Jeżeli ktoś może mi pomóc to będę wdzięczny:)

1. plik index.php
  1. <?php
  2. // Find all photos
  3. $photos = Photograph::find_all();
  4. ?>
  5. <?php
  6.  
  7. // 1. the current page number ($current_page)
  8. $page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
  9.  
  10. // 2. records per page ($per_page)
  11. $per_page = 2;
  12.  
  13. // 3. total record count ($total_count)
  14. $total_count = Photograph::count_all();
  15.  
  16.  
  17. // Find all photos
  18. //$photos = Photograph::find_all();
  19.  
  20. $pagination = new Pagination($page, $per_page, $total_count);
  21.  
  22. // Instead of finding all records, just find the records
  23. // for this page
  24. $sql = "SELECT * FROM photographs ";
  25. $sql .= "LIMIT {$per_page} ";
  26. $sql .= "OFFSET {$pagination->offset()}";
  27. $photos = Photograph::find_by_sql($sql);
  28.  
  29. )
  30.  
  31.  
  32.  
  33. ?>
  34. --------------
  35. nawigacja
  36. <?php
  37. if($pagination->total_pages() > 1) {
  38.  
  39. if($pagination->has_previous_page()) {
  40. echo "<a href=\"list_photos.php?page=";
  41. echo $pagination->previous_page();
  42. echo "\">&laquo; Previous</a> ";
  43. }
  44.  
  45. for($i=1; $i <= $pagination->total_pages(); $i++) {
  46. if($i == $page) {
  47. echo " <span class=\"selected\">{$i}</span> ";
  48. } else {
  49. echo " <a href=\"list_photos.php?page={$i}\">{$i}</a> ";
  50. }
  51. }
  52.  
  53. if($pagination->has_next_page()) {
  54. echo " <a href=\"list_photos.php?page=";
  55. echo $pagination->next_page();
  56. echo "\">Next &raquo;</a> ";
  57. }
  58.  
  59. }
  60.  
  61. ?>
  62.  
  63.  


klasa - pagination.php

  1. <?php
  2.  
  3. // This is a helper class to make paginating
  4. // records easy.
  5. class Pagination {
  6.  
  7. public $current_page;
  8. public $per_page;
  9. public $total_count;
  10.  
  11. public function __construct($page=1, $per_page=20, $total_count=0){
  12. $this->current_page = (int)$page;
  13. $this->per_page = (int)$per_page;
  14. $this->total_count = (int)$total_count;
  15. }
  16.  
  17. public function offset() {
  18. // Assuming 20 items per page:
  19. // page 1 has an offset of 0 (1-1) * 20
  20. // page 2 has an offset of 20 (2-1) * 20
  21. // in other words, page 2 starts with item 21
  22. return ($this->current_page - 1) * $this->per_page;
  23. }
  24.  
  25. public function total_pages() {
  26. return ceil($this->total_count/$this->per_page);
  27. }
  28.  
  29. public function previous_page() {
  30. return $this->current_page - 1;
  31. }
  32.  
  33. public function next_page() {
  34. return $this->current_page + 1;
  35. }
  36.  
  37. public function has_previous_page() {
  38. return $this->previous_page() >= 1 ? true : false;
  39. }
  40.  
  41. public function has_next_page() {
  42. return $this->next_page() <= $this->total_pages() ? true : false;
  43. }
  44.  
  45.  
  46. }
  47.  
  48. ?>


dodatkowa klasa

  1. public static function count_all() {
  2. global $database;
  3. $sql = "SELECT COUNT(*) FROM ".self::$table_name;
  4. $result_set = $database->query($sql);
  5. $row = $database->fetch_array($result_set);
  6. return array_shift($row);
  7. }
  8.  

Turson
  1. for($i=1; $i <= $pagination->total_pages(); $i++) {

zamień na
  1. for($i=1; $i <= 10; $i++) {
proton
Cytat(Turson @ 20.09.2013, 15:42:38 ) *
  1. for($i=1; $i <= $pagination->total_pages(); $i++) {

zamień na
  1. for($i=1; $i <= 10; $i++) {


A nie raczej:

  1. $start = $page - 5;
  2. if ($start < 1) $start = 1;
  3. $end = $page+5;
  4. if ($end > $pagination->total_pages()) $end = $pagination->total_pages();
  5.  
  6. for($i=$start; $i <= $end; $i++) {


Pisane z palca bez zastanowienia, ale moim zdaniem lepsze rozwiązanie niz to powyżej, które bez względu na wszystko pokaże tylko 10 pierwszych stron.
Turson
Tak, nie wziąłem pod uwagę, że może być mniej niż 10 stron.
wlodek_789
zadziałało dzięki
na początku jest tak:

  1. 1 2 3 4 5 6 Next ?


a potem

  1. ? Previous 3 4 5 6 7 8 9 10 11 12 13 Next ?


"Tak, nie wziąłem pod uwagę, że może być mniej niż 10 stron."
jak będzie mniej niż dziesięć to pokaże i tak 10?
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.