Tagged with "pagination" http://www.addedbytes.com/feeds/tag-feed/ en Web Development in Brighton - Added Bytes 2006 120 VBScript Pagination http://www.addedbytes.com/blog/code/vbscript-pagination/ 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 [url=http://www.phpbb.com]phpBB[/url], which is probably why the results may look familiar.

function do_pagination(current_page, total_pages) current_page = cint(current_page) dim url dim strPages : strPages = "" dim intMaxPages dim intMinPages dim intPaginI ' We start be building the URL to add the current_page variable to ... by removing the old current_page data dim objRegExp : set objRegExp = new RegExp objRegExp.Pattern = "(&|\?)?current_page=[^&]*(&|$)" objRegExp.IgnoreCase = true objRegExp.Global = true url = objRegExp.replace(request.ServerVariables("SCRIPT_NAME") & "?" & request.ServerVariables("QUERY_STRING"), "$1") set objRegExp = nothing if (right(url, 1) = "&") then url = left(url, len(url)-1) end if if (right(url, 1) = "?") then url = 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) then url = url & "&" else url = url & "?" end if if (total_pages > 10) then if (total_pages > 3) then intMaxPages = 3 else intMaxPages = total_pages end if for intPaginI = 1 to intMaxPages if (intPaginI = current_page) then strPages = strPages & "<strong>" & intPaginI & "</strong>" else strPages = strPages & "<a href=""" & url & "current_page=" & intPaginI & """>" & intPaginI & "</a>" end if if (intPaginI < intMaxPages) then strPages = strPages & ", " end if next if (total_pages > 3) then if ((current_page > 1) and (current_page < total_pages)) then if (current_page > 5) then strPages = strPages & " ... " else strPages = strPages & ", " end if if (current_page > 4) then intMinPages = current_page else intMinPages = 5 end if if (current_page < total_pages - 4) then intMaxPages = current_page else intMaxPages = total_pages - 4 end if for intPaginI = intMinPages - 1 to intMaxPages + 1 if (intPaginI = current_page) then strPages = strPages & "<strong>" & intPaginI & "</strong>" else strPages = strPages & "<a href=""" & url & "current_page=" & intPaginI & """>" & intPaginI & "</a>" end if if (intPaginI < intMaxPages + 1) then strPages = strPages & ", " end if next if (current_page < total_pages - 4) then strPages = strPages & " ... " else strPages = strPages & ", " end if else strPages = strPages & " ... " end if for intPaginI = total_pages - 2 to total_pages if (intPaginI = current_page) then strPages = strPages & "<strong>" & intPaginI & "</strong>" else strPages = strPages & "<a href=""" & url & "current_page=" & intPaginI & """>" & intPaginI & "</a>" end if if (intPaginI < total_pages) then strPages = strPages & ", " end if next end if else for intPaginI = 1 to total_pages if (intPaginI = current_page) then strPages = strPages & "<strong>" & intPaginI & "</strong>" else strPages = strPages & "<a href=""" & url & "current_page=" & intPaginI & """>" & intPaginI & "</a>" end if if (intPaginI < total_pages) then strPages = strPages & ", " end if next end if do_pagination = strPages end function

]]>
Mon, 15 Mar 2004 17:18:00 +0000 http://www.addedbytes.com/blog/code/vbscript-pagination/ Dave Child ,