2004/07/25 | 后台管理--插入数据、分页、评论
类别(asp) | 评论(0) | 阅读(146) | 发表于 01:14
一、插入数据

其中的分类是从数据库中提取出来的。
分类是一个下拉框列表,一般下拉框的代码为
<select name="spart">
<option value="abc">abc</option>
</select>



我们只需要将中间的列表值用asp代码写出来就可以了。将上面的代码变为:

<select name="spart">
<!--#include file="part_list.asp" -->
</select>



其中,包含文件part_list.asp的代码为
其中,包含文件part_list.asp的代码为

<!--#include file="Conn.asp" -->
<%
set rs=server.CreateObject("adodb.recordset")
sqlstr="select * from part"
rs.open sqlstr,conn,1,1
%>
<%do until rs.EOF '读出全部的分类字段
%>
<option value="<%=rs("part")%>"><%=rs("part")%></option>
<%
rs.movenext
loop%>

插入页面文件insert.asp主要的代码为:(到insok.asp执行)
<!--#include file="session.asp" -->
......
<table width="100%" border="0" cellpadding="3" cellspacing="1" style="font-size:13px;">

<form name="form1" method="post" action="insok.asp">
<tr bgcolor="#eeeeee">
<td width="14%" align="right" valign="top">文章标题:</td>
<td width="86%"><input name="title" type="text" id="title" size="50"></td>
</tr>
<tr bgcolor="#eeeeee">
<td align="right" valign="top">作者出处:</td>
<td>
     <input name="author" type="text" id="author" size="30"> 
<select name="spart">
<!--#include file="part_list.asp" -->
</select>
</td>
</tr>
<tr bgcolor="#eeeeee">
<td align="right" valign="top">文章内容:</td>
<td><textarea name="con" cols="90" rows="20" id="con"></textarea></td>
</tr>
<tr bgcolor="#eeeeee">
<td colspan="2">&nbsp;</td>
</tr>
<tr bgcolor="#eeeeee">
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value=" 提 交 ">
<input type="reset" name="Submit2" value=" 重 置 "></td>
</tr>
</form>

</table>

执行页面insok.asp的代码:
<!--#include file="../Conn.asp" -->
<!--#include file="session.asp" -->
<%
title=request.Form("title") '从上个页的表单中得到title的值
author=request.Form("author")
spart=request.Form("spart")
con=request.Form("con")

set rs=server.CreateObject("adodb.recordset")
sqlstr="select * from concent"
rs.open sqlstr,conn,1,3

rs.addnew '添加数据
rs("title")=title '将上面tilte得到的值赋到数据库中的title字段中
rs("author")=author
rs("spart")=spart
rs("con")=con
rs.update
' 当然,还有执行效率高的方法,但这个添加的方法容易理解些。
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>插入文章</title>
</head>
<body>
<font size="2"><a href="insert.asp">继续添加新文章</a> <a href="viewlist.asp">返回文章列表</a></font>
</body>
</html>
<%rs.close
conn.close
set rs=nothing
set conn=nothing
%>

===================================================

二、分页显示文章列表

首先,连接数据库
<!--#include file="Conn.asp" -->
<%
set rs=server.CreateObject("adodb.recordset")
sqlstrc="select * from table"
rs.open sqlstrc,conn,1,1
%>

设置开头部分
<%filepath=request.servervariables("path_info") '设置路径用%>
<%
rs.PageSize = 20 '每页显示记录数
pagecount=rs.PageCount
page=int(request.QueryString ("page"))
if page<=0 then page=1
if page>pagecount then page=pagecount
if request.QueryString("page")="" then page=1
rs.AbsolutePage=page
%>

中间记录显示
<table width="755" border="0" align="center" cellpadding="3" cellspacing="1">
<tr style="font-size:13px;" bgcolor="#CCCCCC">
<td align="center" width="47%"> 文章标题</td>
<td align="center" width="16%"> 作者或出处</td>
<td align="center" width="15%"> 所属分类 </td>
<td width="16%" align="center"> 入库时间 </td>
<td width="6%" align="center">被阅</td>
</tr>
<%
For i = 1 to rs.PageSize
if rs.EOF then
Exit For
end if '利用for next 循环依次读出记录
%>
<tr bgcolor="#EEEEEE">
<td align="left" width="47%" style="font-size:13px;"><a href="viewmore.asp?id=<%=rs("id")%>"><%=rs("title")%></a></td>
<td align="center" width="16%" style="font-size:12px;"><%=rs("author")%> </td>
<td align="center" width="15%" style="font-size:12px;"><%=rs("spart")%> </td>
<td align="center" width="16%" style="font-size:12px;"><%=rs("time")%> </td>
<td align="center" width="6%" style="font-size:12px;"><%=rs("num")%></td>
</tr>
<%
rs.MoveNext
next
%>
</table>

最后分页导航部分

<form action="<%=filepath%>" method="get">
<%if page=1 and not page=pagecount then%>
[共<%=rs.RecordCount%>篇 <b><%=rs.PageSize%></b>篇/页] 
[首 页] [上一页] 
<a href="<%=filepath%>?page=<%=page+1%>">[下一页]</a> 
<a href="<%=filepath%>?page=<%=pagecount%>">[尾 页]</a>
<%elseif page=pagecount and not page=1 then%>
[共<%=rs.RecordCount%>篇 <b><%=rs.PageSize%></b>篇/页] 
<a href="<%=filepath%>?page=1">[首 页]</a> 
<a href="<%=filepath%>?page=<%=page-1%>">[上一页]</a> 
[下一页] [尾 页]
<%elseif page<1 then%>
<font color=red>没有任何记录!</font>
<%elseif page>pagecount then%>
<font color=red>没有任何记录!</font>
<%elseif page=1 and page=pagecount then%>
<%else%>
[共<%=rs.RecordCount%>篇 <b><%=rs.PageSize%></b>篇/页] 
<a href="<%=filepath%>?page=1">[首 页]</a> 
<a href="<%=filepath%>?page=<%=page-1%>">[上一页]</a> 
<a href="<%=filepath%>?page=<%=page+1%>">[下一页]</a> 
<a href="<%=filepath%>?page=<%=pagecount%>">[尾 页]</a>
<%end if%>
 [页次:<font color=red><b><%=page%></b></font>/<%=pagecount%>] 
转到<input name="page" size=5 value="<%=page%>">页
<input type="submit" value="GO!">
</form>


<%if rs.pagecount<>1 and rs.pagecount<>0 then%>'首先判断页总数不为1和0
<%if page>1 then%>
<%if page<rs.pagecount then %>
[<a Href="<%=filepath%>?Page=<% = 1%>">首页</a>]
[<a Href="<%=filepath%>?Page=<% = page -1 %>">上一页</a>]
[<a Href="<%=filepath%>?Page=<% = page + 1%>">下一页</a>]
[<a Href="<%=filepath%>?Page=<% = rs.PageCount%>">尾页</a>]
<%else%>
[<a Href="<%=filepath%>?Page=<% = 1%>">首页</a>]
[<a Href="<%=filepath%>?Page=<% = page -1 %>">上一页</a>]
[下一页] [尾页]
<% end if %>
<%else%>
[首页] [上一页]
[<a Href="<%=filepath%>?Page=<% = page + 1%>">下一页</a>]
[<a Href="<%=filepath%>?Page=<% = rs.PageCount%>">尾页</a>]
<%end if %>
<%else%>
没有任何记录!
<%end if%>
===================================================
三、详细页面

评细页面的操作很简单,就是连接数据库后显示即可。
<!--#include file="Conn.asp" -->
<%dim con_id
con_id=request.QueryString("id") '从上面的列表点击时得么的id值
if con_id="" then '如果直接进入本页,就让con_id值为1
con_id=1
end if
set rs=server.CreateObject("adodb.recordset")
sqlstr="select * from concent where id="&con_id
rs.open sqlstr,conn,1,1
%>
.......
<table width="755" border="0" align="center" cellpadding="3" cellspacing="1" style="font-size:13px;">
<tr align="left">
<td align="center" bgcolor="#CCCCCC" style="font-size:20px;"><b><%=rs("title")%></b></td>
</tr>
<tr style="font-size:12px;">
<td align="center" bgcolor="#eeeeee"><b>作者或出处:</b><%=rs("author")%> <b>入库时间:</b><%=rs("time")%> <b>所属分类:</b><%=rs("spart")%> <b>被阅 </b><%=rs("num")%> 次</td>
</tr>
<tr>
<td height="10"></td>
</tr>
<tr>
<td bgcolor="#eeeeee" style="font-size:13px;">
<table width="742" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width=742 style="word-break:break-word;line-height:120%; font-size:13px;"><%=rs("con")%></td>
</tr>
</table></td>
</tr>
</table>



其中,有些复杂的可能就是阅读次数的计算了
在显示页面的开头加入以下代码即可
<%set rs1=server.CreateObject("adodb.recordset")
sqlstr1="select * from concent where id="&con_id ' 注意设置SQL语句
rs1.open sqlstr1,conn,1,3
'阅读次数
dim no
no=rs1("num")
no=no+1 '每打开本页一次就加一
rs1("num")=no
rs1.update
rs1.close
%>
==================================================
四、评论操作

评论显示一般地都放在文章显示的下方。
由于,评论可以有多条,所以我们将评论放在单独的一张表reply中。
1, 显示评论
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
function HTML(Str)
if not isnull(Str) then
Str=trim(Str)
Str=replace(Str,"<","&lt;")
Str=replace(Str,">","&gt;")
Str=replace(Str,"'","""")
Str=replace(Str,vbCrLf&vbCrLf,"</p><p>")
Str=replace(Str,vbCrLf,"<br>")
HTML=replace(Str," ","&nbsp;")
end if
end function
</SCRIPT>
<b>·网友评论内容·</b>
<%
set rs2=server.CreateObject("adodb.recordset")
sqlstr2="select * from reply where con_id="&con_id
'注意SQL语句的设置,请查看前一篇的数据库设置。变量con_id的值在上面的详细页面说明时就可以获得
rs2.open sqlstr2,conn,1,1

if rs2.Eof or rs2.Bof then
%>
暂时没有评论!<br>
<%end if
do until rs2.Eof '评论显示部分
%>
<table width="90%" border="0" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#EEEEEE"> <B><%=rs2("rep_name")%></B>网友评论说:</td>
</tr>
<tr>
<td bgcolor="#FFFFFF"><%=html(rs2("rep_con"))%></td>
</tr>
</table><br>
<%rs2.MoveNext
loop
%>


在最上面有一段js代码,是为让显示正常。(这不UBB的显示方式,有兴趣的朋友可以改成UBB的方式。)
将rs2("rep_con")改成html(rs2("rep_con"))就OK了!

2, 加入评论

<form name="form1" method="post" action="plun.asp" id="form1" onSubmit="return check_form()">
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="font-size:13px; ">
<tr bgcolor="#CCCCCC">
<td height="20" colspan="2" style="font-size:16px; "><b>·请发表评论·</b></td>
</tr>
<tr>
<td colspan="2" align="right" valign="top">&nbsp;</td>
</tr>
<tr>
<td width="8%" align="right" valign="top">用 户: </td>
<td width="92%" valign="top"><input name="rep_name" type="text" class="table" id="rep_name"></td>
</tr>
<tr>
<td align="right" valign="top">评 论: </td>
<td valign="top"><textarea name="rep_con" cols="50" rows="6" class="table" id="textarea"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="hidden" name="id" value="<%=con_id%>">
<input name="Submit" type="submit" class="table" value=" 提 交 "> 
<input name="Submit2" type="reset" class="table" value=" 重 置 "></td>
</tr>
</table>
</form>


在倒数第6行加入了一个隐藏域,为传递con_id值到reply表中,这样,才能用上面的SQL语句将对应的评论选出来

需要到plun.asp页去处理

需要到plun.asp页去处理

<!--#include file="Conn.asp" -->
<html>
<head>
<%
rep_name=request.form("rep_name")
con_id=request.form("id")
rep_con=request.form("rep_con")

if con_id<>"" or rep_con<>"" then
' 如果成功添加,设置2秒的时间来跳转
%>
<meta http-equiv="refresh" content="2;url=/viewmore.asp?id=<%=con_id%>">
<%else '不成功,直接回到原页
response.redirect "/viewmore.asp?id=<%=con_id%>"
end if%>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>评论--风的思念</title>
</head>
<body><center>
<%if con_id<>"" or rep_con<>"" then
' 由于本页不能用session来限制,所以用这种方法能限制,只有从详细页的表单输入值提交,rep_con才可能不为空,这样就执行添加评论的代码。而前面的if语句是用来设置跳转的。

set rs=server.CreateObject("adodb.recordset")
sqlstr="select * from reply"
rs.open sqlstr,conn,1,3

rs.addnew
rs("id")=con_id
rs("rep_name")=rep_name
rs("rep_con")=rep_con
rs.update
%>
<font color="#0000FF"><b>评论成功! 你将在2秒后返回!</b></font><br><br>
<font size="2"><a href="/viewmore.asp?id=<%=con_id%>">如果不想等待自动返回,请手动点击本行返回!</a></font>
<%end if%>

</body>
</html>

0

评论Comments

日志分类
首页[223]
javascript[29]
asp[62]
常识[12]
SQL[8]
文学[2]
Dreamweaver[22]
网页设计[25]
flash[20]
片言碎语[21]
php[6]
web standard[16]