login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Navigatie systemen > phpBB like navigatie

phpBB like navigatie

Auteur: Richard - 08 januari 2006 - 10:30 - Gekeurd door: nemesiskoen - Hits: 7270 - Aantal punten: 4.00 (8 stemmen)





Simpele functie, je roept het aan met navigation ($_SERVER['REQUEST_URI'], 150, 5), dan krijg je dus een lijst met pagina's, maar niet allemaal, dus als je 100 pagina's hebt, wordt je layout niet verkloot.

uitleg hoe het werkt staat zo ongeveer wel in het script.

Code:
  1. <?php
  2. /**
  3.  * a simple navigation, without
  4.  * too much characters, so your
  5.  * layout won't be screwed
  6.  * @param string page_url
  7.  * @param integer total
  8.  * @param integer limit
  9.  * @return string
  10.  * @author JeXuS <jexus@jexus.net>
  11. **/
  12. function navigation ($page_url, $total, $limit)
  13. {
  14. // use sprintf, so you can easily edit this
  15. static $href = '<a href="%s">%s<a> ';
  16. // parameter used in url
  17. static $param = 'page';
  18. $param_preg = preg_quote ($param);
  19.  
  20. // erase (&|?)page from the url (for extra security use a while)
  21. while (preg_match ('|[?&]' . $param_preg . '=[^&]*|i', $page_url))
  22. {
  23. // if it matches, replace it with nothing
  24. $page_url = preg_replace ('|[?&]' . $param_preg . '=[^&]*|i', '', $page_url);
  25. }
  26. // check if we have a correct URI (if it doesn't have a ?, we should replace 1 & with ?
  27. $page_url = (strpos ($page_url, '?') !== false) ? $page_url : preg_replace ('~&~', '?', $page_url, 1);
  28. // check if we have a '?', if so, the seperator is '&amp;'
  29. $seperator = (strpos ($page_url, '?') !== false) ? '&amp;' : '?';
  30.  
  31. // the total pages
  32. $pages = ceil ($total / $limit);
  33. // check if the parameter was set
  34. $curr = (isset ($_GET[$param])) ? $_GET[$param] : 1;
  35. // check if the current page isn't too high or low
  36. $curr = ($curr < 1) ? 1 : (($curr > $pages) ? $pages : $curr);
  37. // check the middle, we always need 3 parts, so current might be too low
  38. $middle = ($curr < 7 || $curr > ($pages - 7)) ? round ($pages / 2) : false;
  39. // if the check wasn't false, create an array 'in the middle'
  40. $middle = ($middle !== false) ? array_keys (array_fill ($middle - 2, 4, '')) : array ();
  41. // create an array of integers we'll need
  42. $show = array_merge (
  43. array_keys (array_fill (0, 4, '')), // first set
  44. array_keys (array_fill ($curr - 2, 4, '')), // current set
  45. $middle, // middle set
  46. array_keys (array_fill ($pages - 4, 4, '')) // final set
  47. );
  48. // erase the doubles
  49. $show = array_unique ($show);
  50. // do a natural sort (1 2 3 etc.)
  51. // normally would be 1 10 11 etc.
  52. natsort ($show);
  53.  
  54. // initialise for E_NOTICE error
  55. $return = '';
  56. // another init
  57. $prev = 1;
  58. // prev link
  59. $return .= ($curr > 1) ? sprintf ($href, $page_url . $seperator . $param . '=' . ($curr - 1), '&laquo;') : '';
  60. // loop through the pages
  61. foreach ($show as $page)
  62. {
  63. // update the page (pages like 1 to 10,
  64. // not 0 to 9)
  65. $page += 1;
  66. // if it isn't out of bounds
  67. if ($page <= $pages && $page > 0)
  68. {
  69. // check if there's an open space
  70. if ($prev < $page - 1)
  71. {
  72. // if so, seperate nicely
  73. $return .= ' ... ';
  74. }
  75. // update prev counter
  76. $prev = $page;
  77. // append to the return string, check if it's the current page, otherwise a link
  78. $return .= ($page == $curr) ? "<b>{$page}</b> " : sprintf ($href, $page_url . $seperator . $param . '=' . $page, $page);
  79. }
  80. }
  81. // next link
  82. $return .= ($curr < $pages) ? sprintf ($href, $page_url . $seperator . $param . '=' . ($curr + 1), '&raquo;') : '';
  83. // return trimmed
  84. return rtrim ($return);
  85. }
  86.  
  87. // voorbeeld:
  88. echo navigation ($_SERVER['REQUEST_URI'], 50, 5);
  89. ?>
Download code! Download code (.txt)

 Bekijk een voorbeeld van dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Geen reacties (0)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.029s