ASP

Sort arrays

ASP Comments Off
Sort arrays

'--------Begin Function----
Function fnSort(aSort, intAsc)
Dim intTempStore
Dim i, j
For i = 0 To UBound(aSort) - 1
For j = i To UBound(aSort)
'Sort Ascending
If intAsc = 1 Then
If aSort(i) > aSort(j) Then
intTempStore = aSort(i)
aSort(i) = aSort(j)
aSort(j) = intTempStore
End If 'i > j
'Sort Descending
Else
If aSort(i) < aSort(j) Then
intTempStore = aSort(i)
aSort(i) = aSort(j)
aSort(j) = intTempStore
End If 'i < j
End If 'intAsc = 1
Next 'j
Next 'i
fnSort = aSort
End Function 'fnSort
'-------------------------
Dim aUnSort(3), aSorted
aUnSort(0) = 4
aUnSort(1) = 2
aUnSort(2) = 6
aUnSort(3) = 20
'call the function
'second argument:
' * ascending sorted = 1
' * descending sorting = any other chara
' cter
aSorted = fnSort(aUnSort, 1)
Erase aUnSort

SelectionSort Function

ASP Comments Off
SelectionSort Function

<%
Private Function SelectionSort(ByVal unsortedarray)
    Dim Front, Back, I, Loc, Selcom
    Dim Temp, Selswap, Arrsize
    Arrsize = UBound(unsortedarray)
    For Front = 0 To Arrsize - 1
        Loc = Front
        For Back = Front To Arrsize
            Selcom = Selcom + 1
            If unsortedarray(Loc) > _
             unsortedarray(Back) Then
                Loc = Back
            End If
        Next
        Selswap = Selswap + 1
        Temp = unsortedarray(Loc)
        unsortedarray(Loc) = unsortedarray(Front)
        unsortedarray(Front) = Temp
    Next
    SelectionSort = unsortedarray
End Function
%>

Pagination Script in ASP

ASP Comments Off
Pagination Script in ASP

'    iNumPerPage is the number of items to
' display on each page
'    sURL is the page to put with the link
' , sQuerystring is additional qs stuff
'    adodbConnection, adodbCommand, sTable
' are all for the table to get row count from.                                                            *
'                                                                                *
'    Print out the numbers (if any) betwee
' n the "previous" and "next" buttons
'    It'll act like this (current # is in
' bold):
'    1 2 3 4 5 6 7 8 9 <b>10</b> >> next
'    previous << <b>11</b> 12 13 14 15 16 17 18                                    *
'                                                                                *
'' ****************************************

Sub PrintRecordsetNav( iNumPerPage, adodbConnection, adodbCommand, sTable, sURL, sQuerystring )

    Dim iTtlNumItems, iDBLoc, sSqlTemp, iTtlTemp
    Dim iDBLocTemp, sURLBeg, iA, iB, x, iTemp, rsObj

    iDBLoc = CInt(Request("iDBLoc"))
    iTtlNumItems = CInt(Request("iTtlNumItems"))
    ' Get ttl num of items from the database if it's Not already In the QueryString
    If (iTtlNumItems = 0) Then
        Set rsObj = Server.CreateObject("ADODB.Recordset")
        sSqlTemp = "SELECT COUNT(*) FROM " & sTable
        adodbCommand.CommandText = sSqlTemp
        rsObj.Open adodbCommand
        If Not(rsObj.EOF) Then
            iTtlNumItems = rsObj(0)
        End If
        rsObj.Close
        Set rsObj = Nothing
    End If
    iTtlTemp = iTtlNumItems  iNumPerPage   ' this is the number of numbers overall (use the "" To return int)
    iDBLocTemp = iDBLoc  iNumPerPage       ' this is which number we are currently On (use the "" To return int)
    If (sQuerystring <> "") Then
        sURLBeg = "<A href = """ & sURL & "?" & sQuerystring & "&iTtlNumItems=" & iTtlNumItems & "&iDBLoc="
    Else
        sURLBeg = "<A href = """ & sURL & "?iTtlNumItems=" & iTtlNumItems & "&iDBLoc="
    End If

    '***** BEGIN DISPLAY *****
    ' Print the "Previous"
    If (iDBLoc <> 0) Then
        Response.Write sURLBeg & (iDBLoc - iNumPerPage) & """>Previous</A> "
    End If
    ' Print the <<
    If (iDBLocTemp >= iNumPerPage) Then
        Response.Write sURLBeg & (( iDBLocTemp  iNumPerPage ) * iNumPerPage ^ 2) - (iNumPerPage * 9) & """><<</A> "
    End If

    ' Print the numbers in between. Print them out in sets of 10.
    iA = ( iDBLocTemp  iNumPerPage ) * iNumPerPage
    iB = ( iDBLocTemp  iNumPerPage ) * iNumPerPage + iNumPerPage
    For x = iA To iB
        iTemp = (x * iNumPerPage)
        If (iTemp < iTtlNumItems) Then  ' takes care of extra numbers after the overall final number
            If (iDBLoc = iTemp) Then
                Response.Write " <B>[" & x+1 & "]</B>"
            Else
                Response.Write " " & sURLBeg & (x * iNumPerPage) & """>" & x+1 & "</A>"
            End If
        Else
            Exit For
        End If
    Next

    ' Print the >>
    If (iTtlTemp > iDBLocTemp) Then
        If ((iDBLocTemp + iNumPerPage) <= iTtlTemp) Then
            Response.Write " " & sURLBeg & (( iDBLocTemp  iNumPerPage ) * iNumPerPage + iNumPerPage ) * iNumPerPage & """>>></A> "
        End If
    End If
    ' Print the "Next"
    If ((iDBLoc + iNumPerPage) < iTtlNumItems) Then
        Response.Write " " & sURLBeg & (iDBLoc + iNumPerPage) & """>Next</A>"
    End If
    '***** End DISPLAY *****

End Sub

CombSort Function

ASP Comments Off
CombSort Function

<%
Private Function CombSort(ByVal UnsortedArray)
    Dim I, J, Temp, Gap, Swapped
    Dim ArrSize, Combcom, Combswap
    Const Shrink = 1.3
    ArrSize = UBound( UnsortedArray )
    Gap = Arrsize - 1
    Do
        Gap = Int(Gap / Shrink)
        Swapped = True
        Combcom = Combcom + 1
        For J = 0 To Arrsize - Gap
            If UnsortedArray(J) > UnsortedArray(J + Gap) Then
                Temp = UnsortedArray(J)
                UnsortedArray(J) = UnsortedArray(J + Gap)
                UnsortedArray(J + Gap) = Temp
                Swapped = False
                Combswap = Combswap + 1
            End If
        Next
    Loop Until Not Swapped And Gap = 1
    CombSort = UnsortedArray
End Function
%>
CombSort Function

Arrays to store data

ASP Comments Off
Arrays to store data

<html><head>
<title>arraysredim.asp</title>
</head><body bgcolor="#FFFFFF">
<%
' this code
Dim a_array(100)

' this code will fail
x=100
Dim my_array(x)

%>
</body></html>

Assigning an Array size With a variable produces an Error unless the ReDim command Is used. 

<html><head>
<title>arraysredimcorrect.asp</title>
</head><body bgcolor="#FFFFFF">
<%
Dim my_array()
x=100
ReDim preserve my_array(x)
my_array(20)="Hi!"
my_array(40)="How are You"

lowcount=LBound(my_array)
highcount=UBound(my_array)
Response.Write "lowcount=" & lowcount & ";highcount=" & highcount & "<p>"
For counter=lowcount To highcount
Response.Write counter & "   "
Response.Write my_array(counter) & "<br>"
Next

%>
</body></html>