Return to Snippet

Revision: 8271
at September 11, 2008 09:41 by DaveChild


Initial Code
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

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

Initial Description

                                

Initial Title
VBScript Pagination

Initial Tags

                                

Initial Language
ASP