Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Lists

Formatting

Dynamic Lists

by  Kavius  Posted    (Edited  )
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).


[tt][color green]
'*** 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.
'*
'******************************************************************************[/color]
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[color green]
'******************************************************************************[/color]
[/tt]
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):
Code:
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:
Code:
<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.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top