login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Navigatie systemen > [PHP]Navigatie

[PHP]Navigatie

Auteur: Wijnand - 28 juni 2006 - 10:42 - Gekeurd door: XenoX - Hits: 12842 - Aantal punten: 4.40 (5 stemmen)



Ik had al eerder een navigatie-script gemaakt en die heb ik nu aangepast en er toch weer iets "nieuws" van gemaakt.

Ik had altijd het probleem dat ik bijvoorbeeld een paar honderd resultaten had per topic (in een forum), dat je dan een hele rij met getallen zag van 1 tot en met 30. Als je dat dan nog een beetje mooi wilt weergeven kreeg je: [1] [2] [3] [4] [5] t/m [30].

Dat neemt veel ruimte in beslag.

Het script dat ik gemaakt heb is daar een oplossing voor. Als je bijvoorbeeld bij [20] bent, dan geeft hij dit terug:
[eerste] [vorige] [16] [17] [18] [19] [20] [21] [22] [23] [24] [volgende] [laatste]

Code:
  1. <?php
  2. function navigatie($currentpage,$totalpages,$url)
  3. {
  4. $output = '';
  5. $output_num = '';
  6.  
  7. // both = 4 of meer
  8. if (($currentpage - 4) > 0 && ($currentpage + 4) < $totalpages) {
  9. for($a = ($currentpage-4);$a <= ($currentpage+4);$a++) {
  10. if ($a == $currentpage) {
  11. $output_num .= ' <b>[' . $a .']</b> ';
  12. } else {
  13. $output_num .= ' <a href="' . $url . $a . '">' . $a . '</a> ';
  14. }
  15. }
  16. if (($currentpage - 4) == 1) {
  17. $first = false;
  18. } else {
  19. $first = true;
  20. }
  21. if (($currentpage + 4) == $totalpages) {
  22. $last = false;
  23. } else {
  24. $last = true;
  25. }
  26.  
  27. // left = 4 of meer
  28. } elseif (($currentpage - 4) > 0) {
  29. $math = $totalpages - $currentpage;
  30. $start_temp = 9 - $math;
  31. $start = $currentpage - $start_temp;
  32. if ($start < 1) {
  33. $start = 1;
  34. }
  35. for($a = $start;$a <= $totalpages;$a++) {
  36. if ($a == $currentpage) {
  37. $output_num .= ' <b>[' . $a .']</b> ';
  38. } else {
  39. $output_num .= ' <a href="' . $url . $a . '">' . $a . '</a> ';
  40. }
  41. }
  42. $last = false;
  43. $first = true;
  44. // right = 4 of meer
  45. } elseif (($currentpage + 4) < $totalpages) {
  46. if ($totalpages > 9) {
  47. $loop = 9;
  48. } else {
  49. $loop = $totalpages;
  50. }
  51. for($a = 1;$a <= $loop;$a++) {
  52. if ($a == $currentpage) {
  53. $output_num .= ' <b>[' . $a .']</b> ';
  54. } else {
  55. $output_num .= ' <a href="' . $url . $a . '">' . $a . '</a> ';
  56. }
  57. }
  58. $last = true;
  59. $first = false;
  60. // none = 4 of meer
  61. } else {
  62. for($a = 1;$a <= $totalpages;$a++) {
  63. if ($a == $currentpage) {
  64. $output_num .= ' <b>[' . $a .']</b> ';
  65. } else {
  66. $output_num .= ' <a href="' . $url . $a . '">' . $a . '</a> ';
  67. }
  68. }
  69. $last = false;
  70. $first = false;
  71. }
  72. if ($first == true) {
  73. $output .= ' <b><a href="' . $url . '1">[eerste]</a></b> ';
  74. }
  75. if ($currentpage != 1) {
  76. $output .= ' <b><a href="' . $url . ($currentpage-1) .' ">[vorige]</a></b> ';
  77. }
  78. $output .= " &nbsp;&nbsp; " . $output_num . "&nbsp;&nbsp;";
  79. if ($currentpage != $totalpages) {
  80. $output .= ' <b><a href="' . $url . ($currentpage+1) . ' ">[volgende]</a></b> ';
  81. }
  82. if ($last == true) {
  83. $output .= '<b><a href="' . $url . $totalpages . ' ">[laatste]</a></b> ';
  84. }
  85.  
  86. return $output;
  87. }
  88.  
  89. $itemsperpage = 30;
  90. $totalrecords = 1000; // dit kan een aantal records zijn uit de database
  91. $totalpages = ceil($totalrecords/$itemsperpage);
  92. if (!IsSet($_GET['p'])) {
  93. $currentpage = 1;
  94. } else {
  95. $currentpage = ceil($_GET['p']);
  96. }
  97. if ($currentpage < 1 || $currentpage > ceil($totalpages)) {
  98. $output = 'Helaas, deze pagina bestaat niet.';
  99. } else {
  100. $output = navigatie($currentpage,$totalpages,'?p=');
  101. $output .= '<br /><br />Pagina: ' . $currentpage;
  102. }
  103. ?>
  104.  
  105. <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
  106. 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
  107. <html xmlns='http://www.w3.org/1999/xhtml'>
  108. <head>
  109. <title>Navigatie</title>
  110. <style type="text/css">
  111. * {
  112. font-family : verdana, tahoma, sans-serif;
  113. font-size : 12px;
  114. }
  115.  
  116. body {
  117. background-color : #fff;
  118. margin : 10px;
  119. padding : 0px;
  120. }
  121. </style>
  122. </head>
  123. <body>
  124. <?php
  125. echo $output;
  126. ?>
  127. </body>
  128. </html>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (13)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.027s