请先登录 | 注册
MYCSG.CN
从INDEX.ASP的代码讲起 - 文章首页返回版区
■标题:从INDEX.ASP的代码讲起
■作者:IPSC [2003/2/2 13:44:06]
[blue]以下文章中,带颜色的文字都是讲解,其它是原代码,这里用本联盟当前用的1.15版BBSXP的INDEX.ASP为例。[/blue]



<!-- #include file="setup.asp" -->
上面这段代码表示把setup.asp这个文件里面的全部代码调用在此,放在开头;为什么不直接用呢?想想,如果要直接用,那你想修改下数据库的文件名就要每个ASP文件都改,而大家都调用这个的话,那就只要改这个文件里数据库的名字或者路径就行了,是为了方便管理的;不用多讲,这个SETUP.ASP是定义常量及打开数据库的,当然,它还有检查如有无短讯等等一系列功能啦,无论你在哪一页,只要有短讯,都会跳出提示就是国为大家都调用了此文件的缘故。

<%
top
if Request.Cookies("username")="小草" then
%>
<SCRIPT language=JavaScript>
<!-- Begin
alert("老婆万岁!新年快乐!----------爱你的烽");
//--></SCRIPT>
<%
end if

[blue]<% ... %>是普通的HTM常用的java代码与ASP代码的分界标识,ASP和普通的HTM代码可以混用的,以上就是一例,ASP和HTM的不同就是,ASP代码是在服务器执行的,把执行的结果用HTM表示,代码才在用户的浏览器里显示出来,所以你在IE里浏览INDEX.ASP时,查看浏览那页的原代码时,看到的全都是HTM代码,没有一点ASP代码的;因为这是执行后的结果嘛。以上用了一个IF THEN语句这种命令的意思是
if空格...(条件)...空格then---------如果条件满足,就执行下一行的命令
执行的命令
else-------------否则就执行(条件不满足时,这句可以不要也行)
执行的命令
end空格if------------结束,表示这下面的代码与本IF语句无关,这句不能省
如果您要搞插件的话,用得最多的就是这个语句,一定要掌握!
本例的意思是"如果COOIKE里的用户名是小草--=即Request.Cookies("username")="小草"=--,就跳出井告框提示:老婆万岁!新年快乐,好想你!----------爱你的烽。跳出井告的命令显然是普通的HTM语言,这就是个ASP与普通HTM代码混用的实例了[/blue]

if Request.Cookies("username")="蓝色之恋" or Request.Cookies("username")="Bander" or Request.Cookies("username")="邵三笑" or Request.Cookies("username")="idummy" or Request.Cookies("username")="骆驼" then
%>
<script language=vbscript>
MsgBox "欢迎您! <%=Request.Cookies("username")%> ,开发组有您的加入是我们的荣幸!新年快乐!"
</script>
<%
end if

%>
以上也是IF语句同一用法

<style>TABLE{BORDER-TOP:0px;BORDER-LEFT:0px;BORDER-BOTTOM:1px}TD{BORDER-RIGHT:0px;BORDER-TOP:0px}</style>
<title><%=clubname%></title>
这是定义IE标题显示内容的代码,大家经常看代码的话,经常看到<%=clubname%>或者rs("clubname")及rs1("clubname")的用法,有什么不同呢?其实rs("clubname")是读取数据库里相应表格里某个字段的命令,<%=clubname%>则是出现此命令之前在ASP区域里定义过的变量的值,在这里显示出来。这里的值,在本页里找不到定义的命令,所以显然是在setup.asp里已经定义过了

<style>
body {font-family:arial,helvetica,Tahoma; font-size: 9pt; cursor: url('images/index1.cur')}
TD{font-size:9pt}
</style>
<SCRIPT>
var sgImg="images/xnkl1.gif"
var sgWidth=63
var sgHeight=300
var sgLink="http://www.diybbsxp.com"
var sgNS=(document.layers)?true:false
if(sgNS){document.write('<LAYER ID="Corner" WIDTH='+sgWidth+' HEIGHT='+sgHeight+'><A href="'+sgLink+'" target=_blank><IMG src="'+sgImg+'" BORDER=0 WIDTH="'+sgWidth+'" HEIGHT="'+sgHeight+'"></A></LAYER>');}else{document.write('<DIV ID="Corner" STYLE="position:absolute; width:'+sgWidth+'; height:'+sgHeight+'; z-index:9; filter: Alpha(Opacity=70)"><A href="'+sgLink+'" target=_blank><IMG src="'+sgImg+'" BORDER=0 WIDTH="'+sgWidth+'" HEIGHT="'+sgHeight+'"></A></DIV>');}
function StayCorner(){var sgTop;var sgLeft
if(sgNS){sgTop = pageYOffset+window.innerHeight-document.Corner.document.height-10;sgLeft = pageXOffset+window.innerWidth-document.Corner.document.width-10;document.Corner.top = sgTop;document.Corner.left = sgLeft;}else{
sgTop = document.body.scrollTop+document.body.clientHeight-document.all.Corner.offsetHeight-30;sgLeft = document.body.scrollLeft+document.body.clientWidth-document.all.Corner.offsetWidth-5;Corner.style.top = sgTop;Corner.style.left = sgLeft;}
setTimeout('StayCorner()', 50)}
sgDump = StayCorner()
</SCRIPT>
以上就是首页那个图“新年快乐”的代码,文件名是xnkl1.gif

</head>
<table width=750 align="center" border="0">
<tr>
<td vAlign="top"><img src="images/logo.gif" border="0"></td>
<td align="right">


DIY联盟开发组:<%
master=split(administrators,"|")
for i = 1 to ubound(master)-1
response.write "<b><a href=profile.asp?username="+master(i)+">"+master(i)+"</a></b> "
next
%> <br>
以上是显示开发组名单的语句,也就是区长了,
[blue]这里要讲讲response.write "..."这是在ASP区域显示普通HTM代码的命令,要注意的地方是,里面的代码不能换行,如果有换行,那就先把代码都放在同一行再粘上那个位置,再有个要注册的地方,里面的普通HTM代码里的双引号要改为单引号,还有,如果这里面的HTM代码里要显示读取数据库某一字段,不能用<%...%>,而要改用"&...&"(这里的双引号不用改)[/blue]

注册会员:<b><%=associatortol%></b> | 欢迎新会员:<b><a href=profile.asp?username=<%=newassociator%>><%=newassociator%></a></b>
<br><a href="adminlist.asp">管理员列表</a> | <a href="usertop.asp">会员排行榜</a>

[blue]在ASP里很多网址和我们以前知道的HTM的网址有不同,有时见到网址后面还带?id=740&forumid=17&page=0一类的东西,这些值其实是上一页读取而要在这一页用到的值,用来显示或者计算什么或者作为条件读取数据库的相应字段等等.用法是XXX.ASP?A=..&B=..&C=..&D=..[/blue]

</td>
</tr>
</table>
<TABLE cellSpacing=0 width=750 align=center bgColor=a4b6d7 border=0>
<TBODY>
<TR>
<TD height=1></TD></TR></TBODY></TABLE>
<table borderColor="#a4b6d7" height="28" cellSpacing="0" cellPadding="1" width="750" align="center" border="1">
<tr bgColor="#c4d4e5">
<td align="middle" width="35" bgColor="#ecf5ff">
<img src="images/announce.gif" align="middle"></td>
<td width="706" bgColor="#f2f8ff">&nbsp;社区公告: <span style="position:relative;height:10; filter:blur(Add=1,Direction=135,Strength=4)"><a href=# onclick=javascript:open('affiche.asp','','width=400,height=180,resizable,scrollbars')><%=affichetitle%></a></span> [<%=affichetime%>]</td>
</tr>
</table>
<table borderColor="#a4b6d7" cellSpacing="0" cellPadding="1" width="750" align="center" border="1">
<tr bgColor="#c4d4e5" height="25">
<td vAlign="center" align="middle" width="35">状态</td>
<td vAlign="center" align="middle" width="*">论坛</td>
<td vAlign="center" align="middle" width="80">版主</td>
<td vAlign="center" align="middle" width="35">主题</td>
<td vAlign="center" align="middle" width="35">帖子</td>
<td vAlign="center" align="middle" width="140">最后更新</td>
<td vAlign="center" align="middle" width="35">珍藏</td>
</tr>
</table>
<%
sql="select * from class"
rs.Open sql,Conn,1
do while not rs.eof
[blue]这里讲讲,sql="select * from class"意思是打开数据库里名叫CLASS的表,这里没有查找条件,如果有条件就写成sql="select * from class WHERE A=... AND B=..."这里要求同时满足这两个条件,如果只要满足任何一个,把AND改为OR就行了;下面的do while not rs.eof意思是"如果按上面条件打开数据库,有东西就执行...以LOOP为结束。一定要有始有终哟。[/blue]

classimage=2
if ""&rs("id")&""=""&Request.Cookies("classid")&"" then
classimage=1
前面提到在ASP区域定义一个值,classimage=1就是一个例子

end if
%>
<table borderColor="#a4b6d7" height="23" cellSpacing="0" width="750" align="center" border="1">
<tr>
<td bgColor="#ffffff" colSpan="7"><b><font color="#0bc000"> </font><a href="cookies.asp?menu=classid&classid=<%=rs("id")%>"><img src="images/<%=classimage%>.gif" border="0"><%=rs("classname")%></b></a></td>
</tr>
</table>
<%
if ""&rs("id")&""=""&Request.Cookies("classid")&"" or allclass=1 then
sql="select * from bbsconfig where classid="&rs("id")&""
rs1.Open sql,Conn
do while not rs1.eof
masterlist=""
if rs1("moderated")<>"" then
master=split(rs1("moderated"),"|")
for i = 1 to ubound(master)-1
masterlist=""+masterlist+"<a href=profile.asp?username="+master(i)+">"+master(i)+"</a><br>"
next
end if

if rs1("pass")<>"" then
passphoto="<img src=images/lock.gif>"
else
passphoto="<img src=images/on.gif>"
end if

%>
<table borderColor="#a4b6d7" cellSpacing="0" width="750" align="center" border="1">
<tr>
<td align="middle" width="35" bgColor="#ecf5ff">
<%=passphoto%> </td>
<td onmouseover="this.style.backgroundColor='#ECF5FF'" onmouseout="this.style.backgroundColor=''" vAlign="top" bgColor="#f2f8ff">
<table cellSpacing="0" cellPadding="3" width="368" border="0">
<tr>
<td width="362" colspan="2">
<img border="0" src="images/gothis.GIF"> <span style="position:relative;height:18; filter:blur(Add=1,Direction=135,Strength=4)"><a href="forumdisplay.asp?forumid=<%=rs1("id")%>"><%=rs1("bbsname")%></a></span> <img border="0" src="images/gothis2.GIF"></td>
<td align="center" width="80"><font color="#000000">
<a href="forumdisplay.asp?forumid=<%=rs1("id")%>&TimeLimit=1">
<img alt="查看该论坛当天的帖子" src="images/today.gif" border="0"></a>
<a href="forumdisplay.asp?forumid=<%=rs1("id")%>&search=goodtopic">
<img alt="查看该论坛的精品文章" src="images/goodtopic.gif" border="0"></a>
<a href="newtopic.asp?forumid=<%=rs1("id")%>">
<img alt="在该论坛发表文章" src="images/postdirect.gif" border="0"></a></font></td>
</tr>
<tr>
<td width="65" align="center">
<a href="forumdisplay.asp?forumid=<%=rs1("id")%>"><img border="0" src=<%=rs1("imgshow")%> ></a></td>
<td width="291" align="left" valign="top"><font color="999999"><%=rs1("intro")%></font></td>
</tr>
</table>
</td> <td vAlign="center" align="middle" width="80" bgColor="#ecf5ff">
<table cellSpacing="0" cellPadding="2" width="100%" border="0">
<tr>
<td align="middle"><span style="position:relative;height:18; filter:blur(Add=1,Direction=135,Strength=4)"><%=masterlist%></span>
</td>
</tr>
</table>
</td>
<td vAlign="center" noWrap align="middle" width="35" bgColor="#f2f8ff"><%=rs1("toltopic")%> </td>
<td vAlign="center" noWrap align="middle" width="35" bgColor="#f2f8ff"><%=rs1("tolrestore")%> </td>
<td width=140 bgColor=ecf5ff vAlign=center>
<table width="100%">
<tr>
<td style=word-break:break-all>主题:<span style="position:relative;height:10; filter:blur(Add=1,Direction=135,Strength=4)"><a href="showtopic.asp?id=<%=rs1("lastid")%>&forumid=<%=rs1("id")%>"><%=left(rs1("lasttopic"),8)%></a></span><br>
作者:<a href=profile.asp?username=<%=rs1("lastname")%>><%=rs1("lastname")%></a> <img src="images/lp.gif"><br><%=rs1("lasttime")%></td>
</tr>
</table>
</td>
<td vAlign="center" align="center" width="35" bgColor="#f2f8ff"><SPAN style="CURSOR: hand" onclick="window.external.AddFavorite('<%=cluburl%>forumdisplay.asp?forumid=<%=rs1("id")%>', '<%=rs1("bbsname")%>')"><IMG src="images/addfav.gif" border=0></SPAN>
</td>
</tr>
</table>
<%
rs1.movenext
loop
rs1.close
end if
上面的rs1.movenext表示回到前面,如果按条件打开的数据还有很多,继续读出一下个进行上面的显示或者计算,直到读取完全部的数据才不循环。

rs.movenext
loop
rs.close
%>
<table borderColor="#a4b6d7" cellSpacing="0" cellPadding="0" width="750" align="center" border="1" style="border-left: 0px none; border-top: 0px none; border-bottom: 1px none">
<tr>
<td bgColor="#c4d4e5" height="25">
 ■<b> </b>您的个人状态</td>
</tr>
</table>
<table borderColor="#a4b6d7" cellSpacing="0" cellPadding="1" width="750" align="center" border="1" style="border-left: 0px none; border-top: 0px none; border-bottom: 1px none">
<tr>
<td align="middle" width="35" bgColor="#ecf5ff">
<img src="images/mystate.gif"></td>
<td bgColor="#f2f8ff">
<table cellSpacing="0" cellPadding="3" width="100%" border="0" style="border-left: 0px none; border-top: 0px none; border-bottom: 1px none">
<tr>
<td>

<table border="0" cellpadding="0" cellspacing="0" width="702">

<%
if Request.Cookies("username") <> "" then

sql="select * from user where username='"&Request.Cookies("username")&"'"
rs1.Open sql,Conn


%>
<tr>
<td width="25%"><%=rs1("username")%> - 有 <font face="Georgia, Times New Roman, Times, serif"><%=rs1("newmessage")%> 条新留言</td>
<td width="20%">银两:<font face="Georgia, Times New Roman, Times, serif"><%=rs1("money")%></td>
<td width="20%">体力:<font face="Georgia, Times New Roman, Times, serif"><%=rs1("userlife")%></td>
<td>发表文章:<font face="Georgia, Times New Roman, Times, serif"><%=rs1("posttopic")%></td>
<td>被删文章:<font face="Georgia, Times New Roman, Times, serif"><%=rs1("deltopic")%></td>
</tr>
<tr>
<td width="25%">等级名称:<%=rs1("levelname")%></td>
<td width="20%">存款:<font face="Georgia, Times New Roman, Times, serif"><%=rs1("savemoney")%></td>
<td width="20%">经验:<font face="Georgia, Times New Roman, Times, serif"><%=rs1("experience")%></td>
<td>回复文章:<font face="Georgia, Times New Roman, Times, serif"><%=rs1("postrevert")%></td>
<td>精华文章:<font face="Georgia, Times New Roman, Times, serif"><%=rs1("goodtopic")%>
</td>
</tr>

<%
rs1.close
end if
acturl="index.asp"
bbsname=clubname
forumid=0
%>
<!-- #include file="inc/line.asp" -->
<!-- #include file="inc/useragent.asp" -->
<tr>
<td width="25%">您的IP:<font face="Georgia, Times New Roman, Times, serif"><%=remoteaddr%></td>
<td width="20%">端口:<font face="Georgia, Times New Roman, Times, serif"><%=Request.ServerVariables("REMOTE_PORT")%></td>
<td width="20%">操作系统:<%=strsplit(2)%></td>
<td colspan="2">浏览器:<%=strsplit(1)%></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<%
regonline=conn.execute("Select count(*)from online where username<>''")(0)
openlinelist="打开详细列表"
if Request.Cookies("openlinelist")="1" then
sql="select * from online where username<>'' and eremite<>'1'"
rs.Open sql,Conn,1
do while not rs.eof

if NO_count < 6 then
NO_count=NO_count+1
else
NO_count=1
end if

allline=""&allline&"<td width=16% style=word-break:break-all><a href=# onclick=javascript:open('friend.asp?menu=post&incept="&rs("username")&"','','width=320,height=170')><img src=images/face/"&rs("userface")&".gif width=16 height=16 border=0 alt=发送讯息></a> <a href=profile.asp?username="&rs("username")&" TITLE='目前位置:『"&rs("bbsname")&"』"&vbCrlf&"来访时间:"&rs("cometime")&""&vbCrlf&"活动时间:"&rs("lasttime")&""&vbCrlf&"操作系统:"&rs("userwin")&""&vbCrlf&"浏 览 器:"&rs("userie")&"'>"&rs("username")&"</a></td>"

if NO_count = 6 then
allline=""&allline&"</tr><tr>"
end if

rs.movenext
loop
rs.close
openlinelist="关闭详细列表"
end if


%>
<table borderColor="#a4b6d7" cellSpacing="0" cellPadding="0" width="750" align="center" border="1" style="border-left: 0px none; border-top: 0px none; border-bottom: 1px none">
<tr>
<td bgColor="#c4d4e5" height="25">
 <b>■ </b>在线统计</td>
</tr>
</table>
<table borderColor="#a4b6d7" cellSpacing="0" cellPadding="1" width="750" align="center" border="1" style="border-left: 0px none; border-top: 0px none; border-bottom: 1px none">
<tr>
<td align="middle" width="35" bgColor="#ecf5ff">
<img src="images/online.gif"></td>
<td vAlign="top" bgColor="#f2f8ff">
<table cellSpacing="0" cellPadding="3" width="100%" border="0" style="border-left: 0px none; border-top: 0px none; border-bottom: 1px none">
<tr>
<td height="15">
<font face="Georgia, Times New Roman, Times, serif">目前论坛总共有 <%=onlinemany%> 人在线。其中注册用户 <%=regonline%> 人,访客 <%=onlinemany-regonline%> 人。 [<a href="cookies.asp?menu=openlinelist"><%=openlinelist%></a>]</font></td>
</tr>
</table>
<table border=0 width=100%><tr>
<%=allline%>
</tr>
</table>
</td>
</tr>
</table>

<table borderColor="#a4b6d7" cellSpacing="0" cellPadding="1" width="750" align="center" border="1" style="border-left: 0px none; border-top: 0px none; border-bottom: 1px none">
<tr>
<td bgColor="#c4d4e5" height="25" style="border-right: 0px none; border-top: 0px none" colspan="2" width="742">
 ■<b> </b>友情链接</td>
</tr>
<tr>
<td bgColor="#FFFFFF" width="32" align="center">
<img src="images/shareforum.gif"></td>
<td bgColor="#FFFFFF" width="703" >
<%
sql="select * from link"
rs.Open sql,Conn,1
do while not rs.eof
response.write "<a title='"&rs("name")&""&chr(10)&""&rs("intro")&"' href="&rs("url")&" target=_blank><img src="&rs("logo")&" border=0 width=88 height=31></a> "
rs.movenext
loop
rs.close
%> </td></tr></table>
<%
htmlend
%>

[blue]这次只讲解这些,其它的一般都很少用,自己多看就能理解了。不懂再问吧[/blue]

[注意:此帖子在 2003-2-2 13:47:18 被作者改动过]quot;0" style="border-left: 0px none; border-top: 0px none; border-bottom: 1px none">
<tr>
<td height="15">
<font face="Georgia, Times New Roman, Times, serif">目前论坛总共有 <%=onlinemany%> 人在线。其中注册用户 <%=regonline%> 人,访客 <%=onlinemany-regonline%> 人。 [<a href="cookies.asp?menu=openlinelist"><%=openlinelist%></a>

回复/版区/上篇/下篇/发贴/仅文字/HTML
【倒序/最近21条回复】
■作者:wwwz215 [2003/4/28 14:08:47]
看得我眼花缭乱的!!
还有头晕脑涨的!!

不过,我觉得收获还是蛮大的!
谢谢组长了!

■作者:冰点 [2003/2/17 17:57:56]
谢谢,虽然偶只看懂了1/3,不过发现烽哥哥很爱老婆啊~~

■作者:周周 [2003/2/10 9:28:42]
好帖子,谢谢海烽

■作者:IPSC [2003/2/9 18:46:25]
你明白它的原理就行了,比如人气最强,它就是打开最多人看的贴子,然后一个个列出来,有什么难的,你直接看代码就行了,有什么不懂再在本版发贴提问。

■作者:ddsunday [2003/2/8 15:53:22]
谢谢烽GG
想来想去,还是希望告诉我首页新贴,人气最强等的调用方法

■作者:IPSC [2003/2/7 13:36:27]
<img src=<%=rs1("imgshow")%>> 这里面的IMGSHOW的数据一般是“IMAGE/XX。GIF”
<%=rs1("id")%>这里面的数据一般是1、2、3。。。。
都是用来显示图片的网址,只是按需要的不同显示嘛


[注意:此帖子在 2003-2-7 13:37:33 被作者改动过]

■作者:月影 [2003/2/6 20:01:18]
好东西,这个值得看

■作者:海枫 [2003/2/6 10:27:46]

请解释这句 <img src=<%=rs1("imgshow")%>> 中的imgshow
而用这句 <img src="images/aa<%=rs1("id")%>.gif">
我却能很好理解(aa1.gif-->aa2.gif).

这两句各有什么优缺点?


[注意:此帖子在 2003-2-6 10:40:24 被作者改动过]

■作者:海枫 [2003/2/3 19:10:34]
收到!
最近忙着勾女.我都是通宵看代码.白天睡觉.日子过得似乎很充实.唉,再年轻十岁该多好呀!

■作者:IPSC [2003/2/3 13:52:12]
用不着看书吧,那样很难学会的。先一知半解的学,慢慢在实践中学习。

■作者:海枫 [2003/2/3 2:37:02]


看完其它的教程再结合这里的讲解是有很好的效果.我正在学习中.

■作者:IPSC [2003/2/2 13:52:09]
一般可以同时互不干扰打开数据库里两个表,如rs...及rs1...有什么用呢,如我在RS的表里找一组数据,然后把其中一个值读取出来,作为在RS1里查找数据的条件,这是比较常用的吧,当然用来计算或者显示也有时用到。用的时候,如果执行完时,一定要关掉这个表哟rs.close、rs1.close,如在您关掉RS后,可以再用RS打开另一个表的。

(查看完整版网页)



短讯|首页|登录|算法|电脑版 .
DK MiniBBS Plus v2.0
mm0759.com
691 毫秒 .