Posted By

DaveChild on 09/11/08


Tagged

pagination


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

asifrizvigmailcom


VBScript Pagination


 / Published in: ASP
 

URL: http://www.addedbytes.com/asp/vbscript-pagination/

  1. function do_pagination(current_page, total_pages)
  2.  
  3. current_page = cint(current_page)
  4. dim url
  5. dim strPages : strPages = ""
  6. dim intMaxPages
  7. dim intMinPages
  8. dim intPaginI
  9.  
  10. ' We start be building the URL to add the current_page variable to ... by removing the old current_page data
  11. dim objRegExp : set objRegExp = new RegExp
  12. objRegExp.Pattern = "(&|\?)?current_page=[^&]*(&|$)"
  13. objRegExp.IgnoreCase = true
  14. objRegExp.Global = true
  15. url = objRegExp.replace(request.ServerVariables("SCRIPT_NAME") & "?" & request.ServerVariables("QUERY_STRING"), "$1")
  16. set objRegExp = nothing
  17.  
  18. if (right(url, 1) = "&") then
  19. url = left(url, len(url)-1)
  20. end if
  21.  
  22. if (right(url, 1) = "?") then
  23. url = left(url, len(url)-1)
  24. end if
  25.  
  26. ' Right, now we've got a clean url. Add a character to precede the new current_page value, and off we go!
  27. if (instr(url, "?") > 0) then
  28. url = url & "&"
  29. else
  30. url = url & "?"
  31. end if
  32.  
  33. if (total_pages > 10) then
  34. if (total_pages > 3) then
  35. intMaxPages = 3
  36. else
  37. intMaxPages = total_pages
  38. end if
  39.  
  40. for intPaginI = 1 to intMaxPages
  41. if (intPaginI = current_page) then
  42. strPages = strPages & "<strong>" & intPaginI & "</strong>"
  43. else
  44. strPages = strPages & "<a href=""" & url & "current_page=" & intPaginI & """>" & intPaginI & "</a>"
  45. end if
  46. if (intPaginI < intMaxPages) then
  47. strPages = strPages & ", "
  48. end if
  49. next
  50.  
  51. if (total_pages > 3) then
  52. if ((current_page > 1) and (current_page < total_pages)) then
  53. if (current_page > 5) then
  54. strPages = strPages & " ... "
  55. else
  56. strPages = strPages & ", "
  57. end if
  58.  
  59. if (current_page > 4) then
  60. intMinPages = current_page
  61. else
  62. intMinPages = 5
  63. end if
  64.  
  65. if (current_page < total_pages - 4) then
  66. intMaxPages = current_page
  67. else
  68. intMaxPages = total_pages - 4
  69. end if
  70.  
  71. for intPaginI = intMinPages - 1 to intMaxPages + 1
  72. if (intPaginI = current_page) then
  73. strPages = strPages & "<strong>" & intPaginI & "</strong>"
  74. else
  75. strPages = strPages & "<a href=""" & url & "current_page=" & intPaginI & """>" & intPaginI & "</a>"
  76. end if
  77. if (intPaginI < intMaxPages + 1) then
  78. strPages = strPages & ", "
  79. end if
  80. next
  81.  
  82. if (current_page < total_pages - 4) then
  83. strPages = strPages & " ... "
  84. else
  85. strPages = strPages & ", "
  86. end if
  87. else
  88. strPages = strPages & " ... "
  89. end if
  90.  
  91. for intPaginI = total_pages - 2 to total_pages
  92. if (intPaginI = current_page) then
  93. strPages = strPages & "<strong>" & intPaginI & "</strong>"
  94. else
  95. strPages = strPages & "<a href=""" & url & "current_page=" & intPaginI & """>" & intPaginI & "</a>"
  96. end if
  97. if (intPaginI < total_pages) then
  98. strPages = strPages & ", "
  99. end if
  100. next
  101. end if
  102. else
  103. for intPaginI = 1 to total_pages
  104. if (intPaginI = current_page) then
  105. strPages = strPages & "<strong>" & intPaginI & "</strong>"
  106. else
  107. strPages = strPages & "<a href=""" & url & "current_page=" & intPaginI & """>" & intPaginI & "</a>"
  108. end if
  109. if (intPaginI < total_pages) then
  110. strPages = strPages & ", "
  111. end if
  112. next
  113. end if
  114.  
  115. do_pagination = strPages
  116. end function

Report this snippet  

You need to login to post a comment.