INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Formatting

Dynamic Lists by kavius
Posted: 10 Feb 03 (Edited 11 Feb 03)

I was having a discussion with a friend of mine about the best use of JSP tools this weekend, in particular JSP Tag Libraries (http://java.sun.com/products/jsp/taglibraries.html).

It came to mind that I should really look over my ASP to find areas where something like this would be useful. I found one right away.

On every page I, somewhere on the page, I am bound to make a list from a recordset (OK, so maybe this is a little inefficient, but...). While I don't have the neat and tidy sytax of JSP Tag Libraries, there is nothing stopping me from using a function and passing in a string (versus using an html like syntax).



'*** HTMLRepeater *************************************************************
'*
'*   Desc: Repeats a given HTML string for each record in a recordset,
'*   ^^^^  replacing identifier tags with values from the records.
'*
'*  Input: Valid HTML string to be repeated.
'*  ^^^^^  Recordset to pull data from.
'*
'* Output: none
'* ^^^^^^  
'*
'*  Usage: The html string should contain tags of the format <?FieldName?>.
'*  ^^^^^  Every occurance of this tag will be replaced with the value in the
'*         current record which corresponds to the specified field.
'*
'******************************************************************************

public sub htmlRepeater(html, sql)
    dim rsRepeat
    dim outStr
    dim fld
    dim arrVal()
    dim curVal
    dim numVal
    
    numVal = 0
    
    set rsRepeat = server.CreateObject("ADODB.Recordset")
    rsRepeat.CursorLocation = ADODB.adUseClient
    rsRepeat.Open sql, Application("cnnStr"),adOpenKeyset,adLockReadOnly,ADODB.adcmdtext
    set rsRepeat.ActiveConnection = nothing

    'parse the html string to find all variable tags
    outStr = html
    curVal = 1
    while(curVal > 0)
        'find the start of the first variable
        curVal = len(outStr) - instr(1,outStr,"<?")-1
        'cut everything else away
        outStr = right(outStr,curVal)
        'find the end of the variable
        curVal = instr(1,outStr,"?>")-1
        if(curVal > 0)then
            'add another element to the array
            numVal = numVal + 1
            redim preserve arrVal(numVal)
            'assign the variable to field list
            arrVal(numVal-1) = left(outStr, curVal )
            'cut away the variable
            outStr = right(outStr,len(outStr) - curVal)
        end if
    wend
    
    'display the html for each record
    while (not rsRepeat.EOF)
        outStr = html
        'replace each variable tag with corresponding field
        for curVal = 0 to numVal-1
            outStr = replace(outStr,"<?" & arrVal(curVal) & "?>", rsRepeat(arrVal(curVal)).value)
        next
        Response.Write(outStr)
        rsRepeat.movenext
    wend
    
    rsRepeat.Close
    set rsRepeat = nothing
    
end sub
'******************************************************************************


An example of this would be a situation where you have a list of provinces and want to create a select list from this list of provinces (I'm Canadian, so you are getting Canadian provinces):

dim rec
dim sql

rec = "  <option value='pID'><?pName?></option>" & vbnewline
sql = "select pID, pName from Provinces order by pName "
Response.Write ("<select>")
htmlRepeater rec, sql
Response.Write ("</select>")


Output:

<select>
  <option value='0'>Alberta</option>
  <option value='1'>British Columbia</option>
  <option value='3'>Manitoba</option>
  <option value='6'>New Brunswick</option>
  <option value='8'>Newfoundland</option>
  <option value='10'>Northwest Territories</option>
  <option value='7'>Nova Scotia</option>
  <option value='12'>Nunavit</option>
  <option value='4'>Ontario</option>
  <option value='9'>Prince Edward Island</option>
  <option value='5'>Quebec</option>
  <option value='2'>Saskatchewan</option>
  <option value='11'>Yukon</option>
</select>

This is not limited to only select lists. Because the formatting is defined for each call, you could use this for the formatting of any recordset data for web display.

Back to Microsoft: ASP (Active Server Pages) FAQ Index
Back to Microsoft: ASP (Active Server Pages) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close