One of the first problems many web developers faces is that of pagination. A database query can sometimes results in hundreds, maybe thousands, of results, and they must all be displayed. Without pagination, those results create a huge, unusable mess for any user.
The problem with any automated pagination script is the preservation of variables. There is no point displaying 10 search results on each page of a result set if a site cannot remember what you searched for from one page to the next. This can one very repetitive part of programming web pages - the preservation of variables.
The following script will work around that problems. There is one downside - you must use querystring variables, rather than form variables (set a form to "GET" rather than "POST"), as otherwise the script will not retain the variables from one page to the next.
Other than that, it is very very simple to use. Simply include it on (for example) a search results page, and call it as normal (replacing current_page with the number of the current page, and total_pages with the total number of pages to display, or the variables holding those pieces of information), and the rest will be done for you.
If you wish to use something other than "current_page" as the variable name for the page number in the querystring, you will also need to change that in the script. The easiest way would be to do a simple find and replace on "current_page", though please do back your work up before doing any 'find and replace' work.
call do_pagination(current_page, total_pages)
The following code is based upon the PHP pagination code used in phpBB, which is probably why the results may look familiar.
function do_pagination(current_page, total_pages)current_page = cint(current_page)dim urldim strPages : strPages = ""dim intMaxPagesdim intMinPagesdim intPaginI' We start be building the URL to add the current_page variable to ... by removing the old current_page datadim objRegExp : set objRegExp = new RegExpobjRegExp.Pattern = "(&|\?)?current_page=[^&]*(&|$)"objRegExp.IgnoreCase = trueobjRegExp.Global = trueurl = objRegExp.replace(request.ServerVariables("SCRIPT_NAME") & "?" & request.ServerVariables("QUERY_STRING"), "$1")set objRegExp = nothingif (right(url, 1) = "&") thenurl = left(url, len(url)-1)end ifif (right(url, 1) = "?") thenurl = left(url, len(url)-1)end if' Right, now we've got a clean url. Add a character to precede the new current_page value, and off we go!if (instr(url, "?") > 0) thenurl = url & "&"elseurl = url & "?"end ifif (total_pages > 10) thenif (total_pages > 3) thenintMaxPages = 3elseintMaxPages = total_pagesend iffor intPaginI = 1 to intMaxPagesif (intPaginI = current_page) thenstrPages = strPages & "<strong>" & intPaginI & "</strong>"elsestrPages = strPages & "<a href=""" & url & "current_page=" & intPaginI & """>" & intPaginI & "</a>"end ifif (intPaginI < intMaxPages) thenstrPages = strPages & ", "end ifnextif (total_pages > 3) thenif ((current_page > 1) and (current_page < total_pages)) thenif (current_page > 5) thenstrPages = strPages & " ... "elsestrPages = strPages & ", "end ifif (current_page > 4) thenintMinPages = current_pageelseintMinPages = 5end ifif (current_page < total_pages - 4) thenintMaxPages = current_pageelseintMaxPages = total_pages - 4end iffor intPaginI = intMinPages - 1 to intMaxPages + 1if (intPaginI = current_page) thenstrPages = strPages & "<strong>" & intPaginI & "</strong>"elsestrPages = strPages & "<a href=""" & url & "current_page=" & intPaginI & """>" & intPaginI & "</a>"end ifif (intPaginI < intMaxPages + 1) thenstrPages = strPages & ", "end ifnextif (current_page < total_pages - 4) thenstrPages = strPages & " ... "elsestrPages = strPages & ", "end ifelsestrPages = strPages & " ... "end iffor intPaginI = total_pages - 2 to total_pagesif (intPaginI = current_page) thenstrPages = strPages & "<strong>" & intPaginI & "</strong>"elsestrPages = strPages & "<a href=""" & url & "current_page=" & intPaginI & """>" & intPaginI & "</a>"end ifif (intPaginI < total_pages) thenstrPages = strPages & ", "end ifnextend ifelsefor intPaginI = 1 to total_pagesif (intPaginI = current_page) thenstrPages = strPages & "<strong>" & intPaginI & "</strong>"elsestrPages = strPages & "<a href=""" & url & "current_page=" & intPaginI & """>" & intPaginI & "</a>"end ifif (intPaginI < total_pages) thenstrPages = strPages & ", "end ifnextend ifdo_pagination = strPagesend function
AddedBytes.com is the online playground of 