Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Pager
Forum PHP.pl > Forum > PHP
Balas
Chcialem napisac sobie klase dzielaca wyniki na strony. Narazie jestem na etapie tworzenia linkow i tu zaczyna sie problem.

  1. <?php
  2. class pager
  3. {
  4. /* przewijanie */
  5. var $_home = '|&lt;';
  6. var $_prev = '&lt;';
  7. var $_next = '&gt;';
  8. var $_end = '&gt;|';
  9. /*dane odpowiedzialne za wyglad linkow*/
  10. var $_before;
  11. var $_after;
  12. /*classa linkow*/
  13. var $_class;
  14. /* wynikow na strone*/
  15. var $_recordonpage = '2';
  16.  
  17. /* inne :] */
  18. var $page_id;
  19. var $row_number;
  20. var $page_number;
  21. /* linki */
  22. var $home_link;
  23. var $prev_link;
  24. var $links;
  25. var $next_link;
  26. var $end_link;
  27. /*konstruktor*/
  28. function pager($row_number, $page_id='1')
  29. {
  30. $this->page_number = ($row_number / $this->_recordonpage) - (($row_number % $this->_recordonpage) / $this->_recordonpage) + 1;
  31. $this->row_number = $row_number;
  32. $this->page_id = $page_id;
  33. $this->render_class();
  34. }
  35.  
  36. [...]
  37.  
  38. function setClass($class)
  39. {
  40. if($class!=='') $this->_class=$class;
  41. }
  42.  
  43. [...]
  44.  
  45. /*tworzenie klas dla linkow*/
  46. function render_class()
  47. {
  48. if($this->_class!=='') {
  49. $this->_class = ' class="'.$this->_class.'"'; }
  50. else{
  51. $this->_class='';}
  52. }
  53. /*home*/
  54. function home()
  55. {
  56. if($this->page_id > 1) {
  57. $this->home_link = '<a href="?page_id='.$this->home_id.'"'.$this->_class.'>'.$this->_home.'</a> '; }
  58. }
  59. /*prev*/
  60. function prev()
  61. {
  62. if($this->prev_id > 0) {
  63. $this->prev_link = '<a href="?page_id='.$this->prev_id.'"'.$this->_class.'>'.$this->_prev.'</a> '; }
  64. }
  65. /*generowanie linkow*/
  66. function links()
  67. {
  68. $page_id=1;
  69. while($page_id <= $this->page_number) {
  70. /*aktualna strona*/
  71. if($page_id==$this->page_id) {
  72. $this->links .= '<b>'.$page_id.'</b> ';
  73. }
  74. /*inne linki*/
  75. else {
  76. $this->links .= '<a href="?page_id='.$page_id.'"'.$this->_class.'>'.$page_id.'</a> '; 
  77. }
  78. $page_id++;
  79. }
  80. }
  81. /*next*/
  82. function next()
  83. {
  84. if($this->next_id <= $this->page_number) {
  85. $this->next_link = '<a href="?page_id='.$this->next_id.'"'.$this->_class.'>'.$this->_next.'</a> '; }
  86. }
  87. /*end*/
  88. function end()
  89. {
  90. if($this->page_id < $this->end_id) {
  91. $this->end_link = '<a href="?page_id='.$this->end_id.'"'.$this->_class.'>'.$this->_end.'</a> '; }
  92. }
  93. /*tworzenie pagera*/
  94. function render()
  95. {
  96. $this->render_class();
  97. $this->nav();
  98. $this->home();
  99. echo $this->home_link;
  100. $this->prev();
  101. echo $this->prev_link;
  102. $this->links();
  103. echo $this->links;
  104. $this->next();
  105. echo $this->next_link;
  106. $this->end();
  107. echo $this->end_link;
  108. }
  109. }
  110. ?>


Gdy wywoluje to tak:
  1. <?php
  2. $pager=new pager($row_number, $page_id);
  3. $pager -> setClass('nawigacja');
  4. $pager -> render();
  5. ?>

Wszystko jest OK, a gdy pomijam setClass (klasa dla linku) to sie pieprzy i robi mi takie linki:
  1. <a href="?page_id=2" class=" class=""">2</a>


Probowalem to sam rozwiazac ale nie doszedlem do bledu. Moglby ktos pomoc ?

EDIT
Znalazlem byka tongue.gif
bendi
Masz 2 wywołania render_class - pierwsze w konstrutorze, drugie w metodzie render. Wywołanie w konstruktorze ustawia wartość własności _class na class="", także późniejsz wywoływanie methody setClass raczej nic nie daje.

Wynika to z tego, że porównujesz wartość własności _class wg identyczności, czyli musi być niepustym ciągiem i stringiem, nastomiast inicjowana ona jest na NULL, więc pierewsze wywołanie render_class ustawia niepoprawnie jej wartość, a późniejsze wywołania setClass nie mają już znaczenia.

Proponowany fix:
  1. <?php
  2. class pager {
  3. /* przewijanie */
  4. var $_home = '|&lt;';
  5. var $_prev = '&lt;';
  6. var $_next = '&gt;';
  7. var $_end = '&gt;|';
  8. /*dane odpowiedzialne za wyglad linkow*/
  9. var $_before;
  10. var $_after;
  11. /*classa linkow*/
  12. var $_class;
  13. /* wynikow na strone*/
  14. var $_recordonpage = '2';
  15.  
  16. /* inne :] */
  17. var $page_id;
  18. var $row_number;
  19. var $page_number;
  20. /* linki */
  21. var $home_link;
  22. var $prev_link;
  23. var $links;
  24. var $next_link;
  25. var $end_link;
  26. /*konstruktor*/
  27. function pager($row_number, $page_id = '1') {
  28. $this->page_number = ($row_number / $this->_recordonpage) - (($row_number % $this->_recordonpage) / $this->_recordonpage) + 1;
  29. $this->row_number = $row_number;
  30. $this->page_id = $page_id;
  31. $this->render_class();
  32. }
  33.  
  34. function setClass($class) {
  35. if ($class )
  36. $this->_class. = ' ' . $class;
  37. }
  38.  
  39.  
  40. /*tworzenie klas dla linkow*/
  41. function render_class() {
  42. if ($this->_class) {
  43. $this->_class = ' class="'.$this->_class.'"';
  44. } else {
  45. $this->_class = '';
  46. }
  47. }
  48. /*home*/
  49. function home() {
  50. if ($this->page_id > 1) {
  51. $this->home_link = '<a href="?page_id='.$this->home_id.'"'.$this->_class.'>'.$this->_home.'</a> ';
  52. }
  53. }
  54. /*prev*/
  55. function prev() {
  56. if ($this->prev_id > 0) {
  57. $this->prev_link = '<a href="?page_id='.$this->prev_id.'"'.$this->_class.'>'.$this->_prev.'</a> ';
  58. }
  59. }
  60. /*generowanie linkow*/
  61. function links() {
  62. $page_id = 1;
  63. while ($page_id <= $this->page_number) {
  64. /*aktualna strona*/
  65. if ($page_id == $this->page_id) {
  66. $this->links .= '<b>'.$page_id.'</b> ';
  67. }
  68. /*inne linki*/
  69. else {
  70. $this->links .= '<a href="?page_id='.$page_id.'"'.$this->_class.'>'.$page_id.'</a> ';
  71. }
  72. $page_id ++;
  73. }
  74. }
  75. /*next*/
  76. function next() {
  77. if ($this->next_id <= $this->page_number) {
  78. $this->next_link = '<a href="?page_id='.$this->next_id.'"'.$this->_class.'>'.$this->_next.'</a> ';
  79. }
  80. }
  81. /*end*/
  82. function end() {
  83. if ($this->page_id < $this->end_id) {
  84. $this->end_link = '<a href="?page_id='.$this->end_id.'"'.$this->_class.'>'.$this->_end.'</a> ';
  85. }
  86. }
  87. /*tworzenie pagera*/
  88. function render() {
  89. $this->render_class();
  90. $this->nav();
  91. $this->home();
  92. echo $this->home_link;
  93. $this->prev();
  94. echo $this->prev_link;
  95. $this->links();
  96. echo $this->links;
  97. $this->next();
  98. echo $this->next_link;
  99. $this->end();
  100. echo $this->end_link;
  101. }
  102. }
  103.  
  104. ?>


Dodałem jeszcze możliwość ustawienia wielu klas, dla jedengo linku - nazwy klas będą oddzielone spacjami.

Jezscze możnaby się pobawić w mały refactoring, bo masz powtarzające się cześci kodu odpowiadające za tworzenie początkowej i końcowej części linków, ale to już zostawę CI jako zadanie domowe winksmiley.jpg
Balas
Jak napisalem w EDIT juz znalazlem blad :] ale dzieki za pomoc.
I pytanie, co tu jeszcze mozna by bylo dodac/zmienic? tongue.gif Czy cos zle robie?

  1. <?php
  2.  
  3. class pager
  4. {
  5. /* przewijanie */
  6. var $_home = '|&lt;';
  7. var $_prev = '&lt;';
  8. var $_next = '&gt;';
  9. var $_end = '&gt;|';
  10. /*classa linkow*/
  11. var $_class = '';
  12. /* wynikow na strone*/
  13. var $_recordonpage = '10';
  14.  
  15. /* inne :] */
  16. var $page_id = '1';
  17. var $row_number;
  18. var $page_number;
  19. /* linki */
  20. var $home_link;
  21. var $prev_link;
  22. var $links;
  23. var $next_link;
  24. var $end_link;
  25. /*konstruktor*/
  26. function pager($row_number, $page_id, $on_page='')
  27. {
  28. if($on_page!=='') $this->setRecordOnPage($on_page);
  29. $this->page_number = ($row_number / $this->_recordonpage) - (($row_number % $this->_recordonpage) / $this->_recordonpage) + 1;
  30. $this->row_number = $row_number;
  31. if($page_id > 0) $this->page_id = $page_id;
  32. }
  33. /*ustalanie wygladu przewijania*/
  34. function setNav($home, $prev, $next, $end)
  35. {
  36. if($home!=='') $this->_home=$home;
  37. if($prev!=='') $this->_prev=$prev;
  38. if($next!=='') $this->_next=$next;
  39. if($end!=='') $this->_end=$end;
  40. }
  41. /*clasa dla linkow*/
  42. function setClass($class)
  43. {
  44. if($class!=='') $this->_class=$class;
  45. }
  46. /*ilosc wynikow na strone*/
  47. function setRecordOnPage($on_page)
  48. {
  49. if($on_page!=='') $this->_recordonpage = $on_page;
  50. }
  51. /*ustalanie id stron*/
  52. function nav()
  53. {
  54. $this->home_id = 1;
  55. $this->next_id = $this->page_id+1;
  56. $this->prev_id = $this->page_id-1;
  57. $this->end_id = $this->page_number;
  58. }
  59. /*tworzenie klas dla linkow*/
  60. function render_class()
  61. {
  62. if($this->_class!=='') {
  63. $this->_class = ' class="'.$this->_class.'"'; }
  64. else{
  65. $this->_class='';}
  66. }
  67. /*home*/
  68. function home()
  69. {
  70. if($this->page_id > 1) {
  71. $this->home_link = '<a href="?page_id='.$this->home_id.'"'.$this->_class.'>'.$this->_home.'</a> '; }
  72. }
  73. /*prev*/
  74. function prev()
  75. {
  76. if($this->prev_id > 0) {
  77. $this->prev_link = '<a href="?page_id='.$this->prev_id.'"'.$this->_class.'>'.$this->_prev.'</a> '; }
  78. }
  79. /*generowanie linkow*/
  80. function links()
  81. {
  82. /* gdy id jest rowne 1 */
  83. if($this->page_id == '1') {
  84. $page_id = 1;
  85. while($page_id <= 3) {
  86. if($page_id==$this->page_id) {
  87. $this->links .= '<b>'.$page_id.'</b> '; }
  88. else {
  89. $this->links .= '<a href="?page_id='.$page_id.'"'.$this->_class.'>'.$page_id.'</a> '; }
  90. $page_id++;
  91. }
  92.  
  93. $this->links .= '... ';
  94. return true;
  95. }
  96. /* gdy id jest rowne ostatniej stronie */
  97. if($this->page_id == $this->end_id) {
  98.  
  99. $this->links .= '... ';
  100.  
  101. $page_id = $this->end_id - 2;
  102. $page_id_end = $this->end_id;
  103. while($page_id <= $page_id_end) {
  104. if($page_id==$this->page_id) {
  105. $this->links .= '<b>'.$page_id.'</b> '; }
  106. else{
  107. $this->links .= '<a href="?page_id='.$page_id.'"'.$this->_class.'>'.$page_id.'</a> '; }
  108. $page_id++;
  109. }
  110. return true;
  111. }
  112. /*w innym wypadku :] */
  113. else {
  114. if($this->page_id >= 4) $this->links .= '... ';
  115. $page_id = $this->page_id - 2;
  116. $page_id_end = $this->page_id + 2;
  117. while($page_id <= $page_id_end) {
  118. if($page_id==$this->page_id) {
  119. $this->links .= '<b>'.$page_id.'</b> '; }
  120. elseif($page_id > 0 && $page_id <= $this->page_number) {
  121. $this->links .= '<a href="?page_id='.$page_id.'"'.$this->_class.'>'.$page_id.'</a> '; }
  122. $page_id++;
  123. }
  124. if($this->page_id <= ($this->page_number - 3)) $this->links .= '... ';
  125. return true;
  126. }
  127. }
  128.  
  129. /*next*/
  130. function next()
  131. {
  132. if($this->next_id <= $this->page_number) {
  133. $this->next_link = '<a href="?page_id='.$this->next_id.'"'.$this->_class.'>'.$this->_next.'</a> '; }
  134. }
  135. /*end*/
  136. function end()
  137. {
  138. if($this->page_id < $this->end_id) {
  139. $this->end_link = '<a href="?page_id='.$this->end_id.'"'.$this->_class.'>'.$this->_end.'</a> '; }
  140. }
  141. /*tworzenie pagera*/
  142. function render()
  143. {
  144. $this->render_class();
  145. $this->nav();
  146. $this->home();
  147. echo $this->home_link;
  148. $this->prev();
  149. echo $this->prev_link;
  150. $this->links();
  151. echo $this->links;
  152. $this->next();
  153. echo $this->next_link;
  154. $this->end();
  155. echo $this->end_link;
  156. }
  157. /*limit*/
  158. function limit()
  159. {
  160. $limit_min = ($this->_recordonpage * ($this->page_id - 1));
  161. return $limit_min;
  162. }
  163. /*wyswietlanie ilosci wynikow na strone*/
  164. function onPage()
  165. {
  166. return $this->_recordonpage;
  167. }
  168. }
  169. ?>


i przykladowe zastosowanie :]

  1. <?php
  2.  
  3. $page_id=$_GET['page_id'];
  4.  
  5. $row_number = $db -> row_number('katalog');
  6.  
  7. $pager=new pager($row_number, $page_id);
  8. $pager -> setClass('nawigacja');
  9. $query='SELECT * FROM katalog LIMIT '.$pager -> limit().', '.$pager -> onPage();
  10. $result = $db -> query($query);
  11. while($row = mysql_fetch_array($result))
  12. {
  13. echo $row['title'];
  14. echo '<br />';
  15. }
  16.  
  17. $pager -> render();
  18.  
  19. ?>


W wyniku wyswietla linki:
Kod
|< < ... 5 6 7 8 9 ... > >|
|< < ... 7 8 9
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.