欢迎光临
我们一直在努力

数据显示函数(asp)-ASP教程,数据库相关

建站超值云服务器,限时71元/月

<%

rem ———————————–

rem 作 者:王勤军 qinjunit@yahoo.com

rem 创作日期:2004-10-12

rem 修改日期:2005年1月24日 星期一

rem ———————————–

函数 实用数据分页显示函数

参数:datasql ———– 当前页面数据的sql语句

参数:countsql ———– 查询总条数的sql语句

参数:page ———– 哪 页

参数:pagesize ———– 页 次

参数:theadstrings ——- 显示表头列名称定义,用“,”分隔,与datasql里面的列名对应。

实 例:=======================================

<!–#include virtual="inc/conn.asp"–>

<!–#include virtual="inc/rw_datapager.asp"–>

<%

dim ipagesize,curpage

ipagesize = 18

curpage = 1

if (request.form <> "") then

if isempty(request.form("p")) then

curpage = 1

elseif isnumeric(request.form("p")) then

curpage = clng(request.form("p"))

end if

end if

showrecords "exec p_show accounts,"&ipagesize&","&curpage&",account_code,account_password,account_serial,account_type,account_money,stock_time","select count(account_code) as total from [accounts]",clng(curpage),ipagesize,"卡号,密码,序列号,卡类型,卡金额,入库时间"

closedb()

% >

sub showrecords(datasql,countsql,page,pagesize,modelstrings,theadstrings)

dim total,rs,datmessages

dim usedatamodel

if (request.form("pagertotal") <> "") then

total = clng(request.form("pagertotal"))

else

total = conn.execute(countsql)(0)

end if

if len(modelstrings)<8 then 模版长度在此定义为8

usedatamodel = false

else

usedatamodel = true

end if

datmessages = datmessages & "<table width=""100%"" border=""0"" align=""center"" cellpadding=""0"" cellspacing=""0"" style=""border-collapse:collapse;word-break:break-all"">"

datmessages = datmessages & "<form name=""frmpager"" id=""frmpager"" method=""post"">"

if clng(total) <> 0 then

set rs = conn.execute(datasql)

rem 非存储过程分页====================

set rs=server.createobject("adodb.recordset")

rs.open datasql,conn,1,1

rs.pagesize=pagesize

rs.absolutepage=page

rem ==================================

dim tharray,colcount,k,thstr,i

i = 1 初始化记数器

colcount = rs.fields.count 获取总列数

if not usedatamodel then 不使用模版操作

if len(theadstrings)<1 then

for k = 0 to (rs.fields.count-1)

thstr = thstr & rs.fields(k).name&","

next

tharray = split((mid(thstr,1,len(thstr)-1)),",")

else

tharray = split(theadstrings,",")

end if

datmessages = datmessages & "<tr bgcolor=""#bfe8fb"">"

for k=0 to (colcount-1)

datmessages = datmessages & "<th class=hyxxtext>"&tharray(k)&"</th>"

next

datmessages = datmessages & "</tr>"

else

datmessages = datmessages & "<tr><td>"

end if

———数据循环开始————

while (not rs.eof and i<pagesize)

if not usedatamodel then

if (i mod 2 =0 ) then

datmessages = datmessages & "<tr bgcolor=""#e1f4fd"">"

else

datmessages = datmessages & "<tr bgcolor=""#ffffff"">"

end if

for k=0 to (colcount-1)

datmessages = datmessages & "<td>"&rs(k)&"</td>"

next

datmessages = datmessages & "</tr>"

else 批量替换模版数据

dim onenotestring

onenotestring = modelstrings

for k=0 to (colcount-1)

if isnull(rs(k)) then

onenotestring = replace(onenotestring,"{$data#"&(k+1)&"}","")

else

onenotestring = replace(onenotestring,"{$data#"&(k+1)&"}",htmlstring(rs(k)))

end if

next

datmessages = datmessages & onenotestring

end if

i=i+1

rs.movenext

wend

rs.close()

set rs = nothing

———-数据循环结束———–

if not usedatamodel then

datmessages = datmessages & "<tr bgcolor=""#f3f3f3""><td colspan="""&(colcount+1)&""" align=""left"" height=""22"" valign=""middle"">"&data_pager(total,page,pagesize)&"</td></tr>"

else

datmessages = datmessages & "</td></tr><tr bgcolor=""#f3f3f3""><td align=""left"" height=""22"" valign=""middle"">"&data_pager(total,page,pagesize)&"</td></tr>"

end if

else

datmessages = datmessages & "<tr bgcolor=""#f3f3f3""><td colspan="""&(colcount+1)&""" align=""center"" height=""120"" valign=""middle"">没有符合要求数据</td></tr>"

end if

datmessages = datmessages & "</form></table>"

response.write(datmessages)

end sub

function data_pager(total,curpage,pagesize)

dim jsgofunction

jsgofunction = "<script language=""javascript"">"&_

"function postpager(n){var obj = document.frmpager;obj.p.value = n;obj.pagercurrent.value = n;obj.submit();}</script>"

dim pstr,jumpstr,totalpage

dim prepage,nextpage

jumpstr = "<input type=text name=p style=width:30px;hight:12px value="&curpage&" class=entxt onkeydown=""if(event.keycode==13){if(docheck(this)){event.returnvalue=false;postpager(this.value);}else{event.returnvalue=false;}}"" >"

if (total mod pagesize > 0) then

totalpage = fix(total/pagesize) + 1

else

totalpage = total/pagesize

end if

if (curpage>totalpage) then curpage=totalpage

if (curpage<1) then curpage = 1

if (curpage=1) then

prepage = "上一页"

else

prepage = "<a href=""javascript:postpager(" &(curpage-1)& ");"">上一页</a>"

end if

if (curpage = totalpage) then

nextpage = "下一页"

else

nextpage = "<a href=""javascript:postpager(" &(curpage+1)& ");"">下一页</a>"

end if

pstr = "<style type=""text/css"">* {font-size:12px;};.entxt {font-size:10px;font-family:verdana}</style>"&jsgofunction &"<script language=""javascript"">function docheck(el){var r=new regexp(""^\\s*(\\d+)\\s*$"");if(r.test(el.value)){if(regexp.$1<1||regexp.$1>"&totalpage&"){alert(""页数超出范围!"");document.all[p].select();return false;}return true;}alert(""页索引无效!"");document.all[p].select();return false;}</script>"

data_pager = pstr & "共 <span class=entxt>"&total&"</span> 条 每页<span class=entxt>"&pagesize&"</span>条 当前<span class=entxt><font color=red class=entxt>"&curpage&"</font>/"&totalpage&"</span>页 <a href=""javascript:postpager(1);"">首页</a> "&prepage&" "& nextpage &" <a href=""javascript:postpager("&totalpage&");"">尾页</a> 跳到"&jumpstr&"页<input type=""hidden"" value="""&total&""" name=""pagertotal""><input type=""hidden"" value="""&curpage&""" name=""pagercurrent"">"

end function

const fsobj = "scripting.filesystemobject"

从物理文件中获取专题模板内容

参数:stemplatefile ————— 模板文件相对路径

返回:该文本文件的内容

function gettemplatecontent(stemplatefile)

dim fso,hf

set fso = server.createobject(fsobj)

set hf = fso.opentextfile(server.mappath(stemplatefile))

gettemplatecontent = hf.readall

hf.close

set hf=nothing

set fo=nothing

end function

生成专题主页面文件

参数:urlpath ————— 文件相对路径

参数:isubcode ————— 专题编号

参数:subcontent ————— 专题内容

返回:生成静态html文件

sub setsubjectfile(urlpath,isubcode,subcontent)

dim fso,hf

set fso = server.createobject(fsobj)

set hf = fso.createtextfile(server.mappath(urlpath)&"/"&isubcode&".html",true)

hf.write subcontent

hf.close

set hf=nothing

set fo=nothing

end sub

获取模板循环内容块

参数 scyclename ———— 循环名称,经测试名称必须为英文名称。

参数 stptcontent ———— 模块内容

说明:

[$titlecycle-s] 循环开始标志

[$titlecontent**] 循环内容,即要替换的内容

[$titlecycle-e] 循环结束标志

以上循环名称为 "titlecycle"

function tpt_cyclecontent(scyclename,stptcontent)

dim ps,pe

ps = instr(1,stptcontent,"[$"&scyclename&"-s]",1) + len("[$"&scyclename&"-s]")

pe = instr(ps,stptcontent,"[$"&scyclename&"-e]",1)

if (pe<=ps) or (ps<=0) or (pe<=0) then

tpt_cyclecontent = "error:not found."

exit function

end if

tpt_cyclecontent = mid(stptcontent,ps,(pe-ps))

end function

清除循环开始和结尾标记

参数 scyclename ———— 循环名称,经测试名称必须为英文名称。

参数 stptcontent ———— 模块内容

function tpt_cycletagclear(scyclename,stptcontent)

if (instr(1,stptcontent,"[$"&scyclename&"-s]",1)>0) and (instr(1,stptcontent,"[$"&scyclename&"-e]",1)>0) then

tpt_cycletagclear = replace(replace(stptcontent,"[$"&scyclename&"-s]",""),"[$"&scyclename&"-e]","")

else

tpt_cycletagclear = stptcontent

end if

end function

清除模板中的循环内容

参数 scyclename ———— 循环名称,经测试名称必须为英文名称。

参数 stptcontent ———— 模块内容

function tpt_cycleclear(scyclename,stptcontent)

if (instr(1,stptcontent,"[$"&scyclename&"-s]",1)>0) and (instr(1,stptcontent,"[$"&scyclename&"-e]",1)>0) then

dim ps,pe

ps = instr(1,stptcontent,"[$"&scyclename&"-s]",1)

pe = instr(ps,stptcontent,"[$"&scyclename&"-e]",1)+ len("[$"&scyclename&"-e]")

if (pe<=ps) or (ps<=0) or (pe<=0) then

tpt_cycleclear = stptcontent

exit function

else

tpt_cycleclear = replace(stptcontent,mid(stptcontent,ps,(pe-ps)),"")

end if

else

tpt_cycleclear = stptcontent

end if

end function

按指定模板内容循环

参数 rsarray ———— 数据集、二维数组 (字段名或字段名索引,数据索引)

参数 replacearray ———— 替换集、二维数组 (待替换的内容,数据集索引,模板规则)

参数 cyclecont ———— 循环模板

说明:

cyclecont可以通过函数 tpt_cyclecontent(scyclename,stptcontent) 获得

replacearray 实例说明

dim rparray(1,2)

rparray(0,0) = "[$piccontent]" 模板中的内容

rparray(0,1) = 1 数据集中的第2列

rparray(0,2) = "<img src=http://www.witol.com/imagefiles/$ border=0>" 模板规则

====模板规则中的$即数据库集中相应列的内容====

rparray(1,0) = "[$pic]" 模板中的内容

rparray(1,1) = 0 数据集中的第2列

rparray(1,2) = "" 不应用模板规则

function tpt_cycle(rsarray,replacearray,cyclecont)

dim i,k,rscount,rpcount

dim midstr,retstrings,rcyclecont

rscount = ubound(rsarray,2)

rpcount = ubound(replacearray)

for i=0 to rscount

用当前数据替换模板内容

for k=0 to rpcount

midstr = rsarray(cint(replacearray(k,1)),i)

if isnull(midstr) then midstr =" "

if k=0 then rcyclecont = cyclecont

if len(replacearray(k,2)) <1 then

rcyclecont = replace(rcyclecont,replacearray(k,0),midstr)

else

rcyclecont = replace(rcyclecont,replacearray(k,0),replace(replacearray(k,2),"$",midstr))

end if

next

retstrings = retstrings & rcyclecont

next

tpt_cycle = retstrings

end function

获取含子循环的数据内容

参数 rsarray ———— 数据集、二维数组 (字段名或字段名索引,数据索引)

参数 replacearray ———— 替换集、二维数组 (待替换的内容,数据集索引,模板规则)

参数 cyclecont ———— 循环模板

参数 childcycle ———— 子循环一维数组 childcycle(含变量的sql语句,对应关系列索引,替换关系二维数组,循环块标记名称)

说明:具体说明参见函数 function tpt_cycle(rsarray,replacearray,cyclecont)

关于chilecycle参数的实例 ================================

dim rparray2(2,2)

rparray2(0,0) = "[$topicid]"

rparray2(0,1) = 0

rparray2(0,2) = ""

rparray2(1,0) = "[$topiccontent]"

rparray2(1,1) = 1

rparray2(1,2) = ""

rparray2(2,0) = "[$topicclass]"

rparray2(2,1) = 2

rparray2(2,2) = ""

dim childcycle(3)

childcycle(0) = "select i_id,i_title,i_class from ls_info_main join listtable on ls_info_main.i_tcode=listtable.listid where listtable.unoffical=0 and i_flag=1 and listtable.listcode like $% order by idcode asc"

childcycle(1) = 0

childcycle(2) = rparray2

childcycle(3) = "topiccycle"

function tpt_multicycle(rsarray,replacearray,cyclecont,childcycle)

dim i,k,rscount,rpcount

dim midstr,retstrings,rcyclecont

rscount = ubound(rsarray,2)

rpcount = ubound(replacearray)

for i=0 to rscount

用当前数据替换模板内容

for k=0 to rpcount

midstr = rsarray(cint(replacearray(k,1)),i)

if isnull(midstr) then midstr=" "

if k=0 then rcyclecont = cyclecont

if len(replacearray(k,2)) <1 then

rcyclecont = replace(rcyclecont,replacearray(k,0),midstr)

else

rcyclecont = replace(rcyclecont,replacearray(k,0),replace(replacearray(k,2),"$",midstr))

end if

next

rem child added

if isarray(childcycle) then

if (ubound(childcycle)=3) then

dim rs,sql,mrsarray

dim mcyclecont,ccycletpt

sql = replace(childcycle(0),"$",rsarray(childcycle(1),i))

ccycletpt = tpt_cyclecontent(childcycle(3),rcyclecont)

set rs = conn.execute(sql)

if not rs.eof then

mrsarray = rs.getrows()

mcyclecont = tpt_multicycle(mrsarray,childcycle(2),ccycletpt,"") get data

rcyclecont = replace(rcyclecont,ccycletpt,mcyclecont) replace template with data

rcyclecont = tpt_cycletagclear(childcycle(3),rcyclecont) clear template tag

else

rcyclecont = tpt_cycleclear(childcycle(3),rcyclecont) clear template

end if

rs.close()

set rs = nothing

end if

end if

rem end

retstrings = retstrings & rcyclecont

next

tpt_multicycle = retstrings

end function

批量替换模板内容

参数 tptcontent ———— 模板内容

参数 replacearray ———— 替换集、二维数组 (待替换的内容,替换内容,模板规则)

说明 模板规则里一般包含替换内容的指定符号"$"

function tpt_rewrite(tptcontent,replacearray)

dim rpcount,i,retstrings

retstrings = tptcontent

rpcount = ubound(replacearray)

for i=0 to rpcount

if (len(replacearray(i,2))<1) then

retstrings = replace(retstrings,replacearray(i,0),replacearray(i,1))

else

retstrings = replace(retstrings,replacearray(i,0),replace(replacearray(i,2),"$",replacearray(i,1)))

end if

next

tpt_rewrite = retstrings

end function

%>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 数据显示函数(asp)-ASP教程,数据库相关
分享到: 更多 (0)

相关推荐

  • 暂无文章