用ASP构建你的网站新闻发布
(一)
更新每天新闻内容,对webmaster们来说是一件很头痛的事,首先,收集了大量的新闻资料后,还必须制作大量的网页,每天大大小小的国际新闻,国内新闻,IT新闻,可真够你累的。最致命的一点,这些松散的新闻是管理不了的,不能查询,不能在线动态删改,新闻讲求时效性,当你作好网页然后上传到服务器上的时侯,恐怕别人已早你一步,把新闻报导出来了。当真吃力不讨好,针对现在我们的上网环境,在线发布新闻,动态生成新闻网页,为新闻添加搜索,查找功能是必不可少的。
那么,使用ASP技术如何来实现动态的新闻发布系统呢?而新闻系统的做法大致上有两种,一就是把录入的新闻内容自动由程序直接生成html文件,比如按日期和新闻ID号来做htm文件的名称,我们常常可以从一些大的新闻站看到这样的处理。而另一种就是直接把新闻数据保存到数据库里,当用户阅读新闻时。从数据库中调出数据,动态生成页面。
两种处理有何差别?第一种的显示速度和对服务器的负何来说,是最轻的。对于大流量的站点,速度就是生命,网站太慢了,有谁去浏缆呢?后一种方法在处理上比教简便。维护也方便,还有一种就是两种双结何,因为只有应用数据库才能方便地处理查询,从而把读者引导至相应的新闻页面,综合上面的方法,可以说都是各有千秋,现在我们主要介绍使用数据库的那种动态页面方法。
从处理流程上来说,主要是提供一个管理新闻的页面(可以添加和删除每天的新闻,包括新闻图片,标题等),然后在到调用新闻的显示页面处理。考滤到新闻需要插到不同风格的网页中,所以不能使用一个固定格式的页面来显示新闻提要。应当把它们做成一个主页的可插入部分,那么就可以在不同风格的页面上调用新闻了。就如下面的script语句
<script language="javascript" src="http//xxx.com.cn/special/linux/lnews/autoup.asp">
</script>
最后还有一个阅读新闻时的详细内容页面。总的来说,也就是一个可以完成在线新闻发布的程序主要由四大部分程序构成
addnew.asp(用于把新闻数据保存到数据库中)delete.asp(用于处理删除新闻)
autoup.asp(用于处理新闻的标题显示,也就是插入主页的部分)read.asp(动态生成新闻的详细内容)
附助处理的部分有set_disp.asp(用于处理新闻在主页的显示数目等等)
当然,必不可少的就是一个新闻数据库了。
(一)构建新闻数据库
分析一下构成每条新闻的元素,主要由新闻标题,新闻内容,图片地址,连接地址,新闻的发布时间组成。而在构建数据库的时候。考虑到处理需要,必需加上新闻的ID号,以便识别。再者的话,可以加上点击统计,以便了解读者感性趣的内容。access数据库是一个非常适合于开发阶段使用的过度时期数据库,因此。我们构造如下的一个新闻数据库(data.mdb)
表data
news_id long 新闻的编号
news_class text 新闻的类别
news_class_name text 新闻的类别名
news_title text 新闻的标题
news_head logic 是否首页显示
news_comment comment 新闻的内容
news_year text 新闻的年份
news_month text 新闻的月份
news_day text 新闻的内容
news_time text 更新时间
news_delete logic 删除标志
按上述的要求建好这个表,当我们经过测试稳定后,就可以把新闻数据库转入SQL server里面
了。好了,接这下来的这步,我们一起来做一个新闻的管理页面,为了布局和管理方便,我们采用
框架结构做出如下的面页。webadmin.htm框架的主文件。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新闻发布系统管理页</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>
<frameset framespacing="0" rows="*,14%">
<frame name="main" target="footnotes" src="webadmin.asp" scrolling="auto">
<frame name="footnotes" src="delete.asp" scrolling="auto">
<noframes>
<body>
<p>This page uses frames, but your browser doesn''t support them.</p>
</body>
</noframes>
</frameset>
</html>
而面包含的文件是webadmin.asp这个文件。主要由它提供录入新闻的每天更新界面,代码如下。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style TYPE="text/css">
<!--
.lh22px {font-size:12px;line-height:22px;font-family:宋体}
a { text-decoration: none}
body {line-height:18px;font-size:9pt;font-family:宋体}
td {line-height:18px;font-size:9pt;font-family:宋体}
a:hover {color:#FF0000;text-decoration:none}
-->
</style>
<base target="footnotes">
<title>新闻发布系统管理页</title>
</head>
<body topmargin="8" leftmargin="8">
<p align="center"><font face="楷体_GB2312" size="6" color="#FF0000"><strong>新闻发布系统WEB管理页</strong></font></p>
<div align="center"><center>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="100%"><form method="POST" action="addnew.asp">
<table border="0" width="100%" height="126" cellpadding="0">
<tr>
<td width="100%" height="9" colspan="2" bgcolor="#E7E7E7"><div align="center"><center><p><font
size="3"><strong>添加每天新闻内容</strong></font></td>
</tr>
<tr align="center">
<td width="15%" height="8" bgcolor="#E7E7E7"><div align="left"><p>新闻标题:(必须)</td>
<td width="85%" height="8" bgcolor="#F9F9F9"><div align="left"><p><input type="text"
name="title" size="73"></td>
</tr>
<tr align="center">
<td width="15%" height="34" bgcolor="#E7E7E7" valign="top"><div align="left"><p>新闻内容:(必须)</td>
<td width="85%" height="34" bgcolor="#F9F9F9"><div align="left"><p><textarea rows="13"
name="comment" cols="72"></textarea></td>
</tr>
<tr align="center">
<td width="15%" height="17" bgcolor="#E7E7E7"><div align="left"><p>图片连接地址:</td>
<td width="85%" height="17" bgcolor="#F9F9F9"><div align="left"><p><input type="text"
name="pic" size="73"></td>
</tr>
<tr align="center">
<td width="15%" height="17" bgcolor="#E7E7E7"><div align="left"><p>新闻类型:(必须)</td>
<td width="85%" height="17" bgcolor="#F9F9F9"><div align="left"><p><select
name="class_name" size="1" value="<%=session("class_name")%>">
<option value="市场风云">市场风云</option>
<option value="IT新闻">IT新闻</option>
<option value="广州市场">广州市场</option>
<option value="保 留">保 留</option>
<option value="保 留">保 留</option>
</select></td>
</tr>
<tr align="center">
<td width="15%" height="17" bgcolor="#E7E7E7"><div align="left"><p>是否首页:</td>
<td width="85%" height="17" bgcolor="#F9F9F9"><div align="left"><p><input type="checkbox"
name="head" value="YES"></td>
</tr>
<tr align="center">
<td width="100%" height="16" colspan="2" bgcolor="#E7E7E7"><div align="center"><center><p><input
type="submit" value="提 交" name="B1"> <input type="reset"
value="清 除" name="B2"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</center></div><div align="center"><center>
<table border="0" width="750" cellpadding="0">
<tr>
<td width="740"></td>
</tr>
<tr>
<td width="740"><form method="POST" action="set_disp.asp">
<table border="0" width="100%" cellpadding="0">
<tr>
<td width="100%" bgcolor="#E7E7E7" colspan="5"><div align="center"><center><p>首页新闻显示条数设置</td>
</tr>
<tr>
<td width="20%"><div align="center"><center><p>广州市场</td>
<td width="20%"><div align="center"><center><p>市场风云</td>
<td width="20%"><div align="center"><center><p>IT新闻</td>
<td width="20%"><div align="center"><center><p>保 留</td>
<td width="20%"><div align="center"><center><p>保 留</td>
</tr>
<tr>
<td width="20%"><div align="center"><center><p><input type="text" name="disp_1" size="10"
value="<%=application("disp_1")%>"></td>
<td width="20%"><div align="center"><center><p><input type="text" name="disp_2" size="10"
value="<%=application("disp_2")%>"></td>
<td width="20%"><div align="center"><center><p><input type="text" name="disp_3" size="10"
value="<%=application("disp_3")%>"></td>
<td width="20%"><div align="center"><center><p><input type="text" name="disp_4" size="10"
value="<%=application("disp_4")%>"></td>
<td width="20%"><div align="center"><center><p><input type="text" name="disp_5" size="10"
value="<%=application("disp_5")%>"></td>
</tr>
<tr>
<td width="100%" bgcolor="#E7E7E7" colspan="5"><div align="center"><center><p><input
type="submit" value="提 交" name="B1"> <input type="reset"
value="清 除" name="B2"></td>
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td width="740"></td>
</tr>
<tr>
<td width="740"><font color="#FF0000">附注</font> 新闻标题:即为显示在主页中的连接标题,因此不可省略。</td>
</tr>
<tr>
<td width="740"> 新闻内容:为点击标题连接后窗口所打开显示的新闻内容,也不允许省略。</td>
</tr>
<tr>
<td width="740"> 新闻图片:新闻包含图片,请把图片地址连接复制在此,以便显示图片</td>
</tr>
<tr>
<td width="740"> 新闻类型:选择新闻在不同栏目中显示的位置</td>
</tr>
<tr>
<td width="740"> 是否首页:如首页的综合新闻包需含图片,请复选此项</td>
</tr>
<tr>
<td width="740"><p align="center"><a href="index.asp" target="_parent">返回新闻首页</a></td>
</tr>
</table>
</center></div>
</body>
</html>
好了,下节“添加每天的新闻内容”将为您介绍如何对新闻数据进行处理,请先按步骤建立好上面的编程环境。
(二)添加和管理每天的新闻内容
当进行了新闻提交后,则交由一个名为addnew.asp的asp程序来对新闻内容进行处理,以便分类和保存,为了显示清析,我们每提交一条新闻,下面的那个新闻内容库就重新读入,以便可以查看新闻是否能成功加入都数据库中,也可以放便地删除新闻内容。
现在看看addnew.asp是如何完成程序处理的。
<% @language="vbscript" %>
<%
response.buffer=true
Response.Expires=0
''保存数据
session("title")=request.form("title")
session("comment")=request.form("comment")
session("pic")=request.form("pic")
session("class_name")=request.form("class_name")
session("head")=request.form("head")
''判断传过来的参数符不符合要求
if request.form("title")="" then
response.redirect "delete.asp"
end if
if request.form("comment")="" then
response.redirect "delete.asp"
end if
if request.form("class_name")="" then
response.redirect "delete.asp"
end if
select case request.form("class_name")
case "市场风云"
class=1
case "IT新闻"
class=2
case "广州市场"
class=3
case "保 留"
class=4
case "保 留"
class=4
end select
''连接数据库
%>
<!--#include Virtual="/news/data/data.inc"-->
<%
Set rs = Server.CreateObject("ADODB.Recordset")
sql="select * from data order by news_id ASC"
rs.open sql,conn,3,2
''查找编号
if rs.bof then
reco=0
else
rs.movelast
reco=Clng(rs("news_id"))+1
end if
rs.addnew
rs("news_id")=reco
if request.form("head")="YES" then
rs("news_head")=-1
end if
rs("news_class")=class
rs("news_class_name")=request.form("class_name")
if request.form("pic")<>"" then
rs("news_pic")=request.form("pic")
rs("news_title")=request.form("title")&"(图文)"
else
rs("news_title")=request.form("title")
end if
if request.form("head")="YES" then
rs("news_head")=-1
end if
tmpmess=request.form("comment")
tmpmess=replace(tmpmess,chr(10)," <br>")
tmpmess=replace(tmpmess,"<","《")
tmpmess=replace(tmpmess,">","》")
tmpmess=replace(tmpmess,"《br》","<br>")
rs("news_comment")=tmpmess
rs("news_year")=year(Date)
rs("news_month")=month(Date)
rs("news_day")=day(Date)
if Instr(Time,"下午")>0 then
tmp=left(right(Time,8),2)+12
rs("news_time")=tmp&right(Time,6)
else
rs("news_time")=tmp&right(Time,8)
end if
rs("news_delete")=0
rs.update
rs.close
response.redirect "delete.asp"
%>
程序中首先判断了提交的新闻是否合符条件,比如新闻标题是不能为空的,而换行符也得转换为html语句的br,否则不能正常地显示出新闻的格式,而这里采用了一个包含文件data.inc,它的用处主要是用于连接不同的数据库,所以asp程序都通过包含这个文件来指定数据源,那么当更改使用别的数据库时,只需要更改这个文件,就能方便地实现来不同数据库间的切换,是不是十分方便呢?
以下的data.inc文件的内容。
<%
Set conn = Server.CreateObject("ADODB.Connection")
DBPath = Server.MapPath("/")&"/news/data/data.mdb"
conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath
%>
还有需要注意一下的是NT和windows 98的日期时间函数所返回的值是不同的,win98是以24小时来计算的,而NT Server则使用12小时,另外加上“上午”和“下午”来表示,因此需要作出少许的更该处理。
程序处理完毕后,重新导向delete.asp这个asp文件,用以刷新新闻数据库内的显示,那么每提交一条新闻资料,我们马上可以看见执行的结果是否成功了。
以下是delete.asp的文件内容。
<% @language="vbscript" %>
<%
response.buffer=true
Response.Expires=0
%>
<!--#include Virtual="/news/data/data.inc"-->
<%
Set rs = Server.CreateObject("ADODB.Recordset")
sql="select * from data order by news_class ASC,news_id Desc"
''rs.PageSize=100
rs.open sql,conn,3,2
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新闻数据库中所存的资料</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<style TYPE="text/css">
<!--
.lh22px {font-size:12px;line-height:22px;font-family:宋体}
a { text-decoration: none}
body {line-height:18px;font-size:9pt;font-family:宋体}
td {line-height:18px;font-size:9pt;font-family:宋体}
a:hover {color:#FF0000;text-decoration:none}
-->
</style>
<meta name="Microsoft Border" content="none">
</head>
<body>
<form method="POST" action="delete.asp">
<table border="0" width="750" cellpadding="0">
<tr>
<td width="750" bgcolor="#EBEBEB" colspan="2"><div align="center"><center><p><font
size="3"><strong>新闻数据库中所存的资料</strong></font></td>
</tr>
<%
do while not rs.eof
%>
<tr align="center">
<td width="112" bgcolor="#EBEBEB">是否删除:<%if rs("news_delete")=-1 then%>
<input type="checkbox" name="<%=rs("news_id")%>"
value="ON">
<%else%>
<input type="checkbox" name="<%=rs("news_id")%>"
value="OFF">
<%end if%>
</td>
<td width="638" bgcolor="#FAFAFA" align="left"><%if rs("news_class")=1 then%>
<font color=red>市场风云</font>
<%end if%>
<%if rs("news_class")=2 then%>
<font color=red>IT新闻</font>
<%end if%>
<%if rs("news_class")=3 then%>
<font color=red>广州市场</font>
<%end if%>
<%if rs("news_class")=4 then%>
<font color=red>保 留</font>
<%end if%>
<%if rs("news_class")=5 then%>
<font color=red>保 留</font>
<%end if%>
<%=rs("news_title")%></td>
</tr>
<%
rs.movenext
loop
%>
<tr align="center">
<td width="100%" colspan="2" bgcolor="#EBEBEB"><center><p><input
type="submit" value="提 交" name="B1"> <input type="reset"
value="清 除" name="B2"></td>
</tr>
</table>
</form>
</body>
</html>
<%
if Request.ServerVariables("REQUEST_METHOD")="POST" then
if not rs.bof then
rs.movefirst
end if
do while not rs.eof
n=trim(cstr(rs("news_id")))
if request.form(n)="OFF" then
rs.delete
rs.update
end if
rs.movenext
loop
response.redirect "delete.asp"
rs.close
end if
response.flush
%>
delete.asp也是一个构成webadmin.htm的管理页面的asp文件,它负责新闻数据库的标题显示,以便让您知道现在库用有些什么新闻。同时可以通过它来删除新闻记录,那么我们在使用新闻的主页里反应出来的结果就是动态的了,新闻可以在线添加和删除。令外一个附加的asp程序
set_diap.asp是用于控制每一类新闻在主页上显示的数目的,它主要使用application对象来设定。
<% @language="vbscript" %>
<%
response.buffer=true
Response.Expires=0
application("disp_1")=request.form("disp_1")
application("disp_2")=request.form("disp_2")
application("disp_3")=request.form("disp_3")
application("disp_4")=request.form("disp_4")
application("disp_5")=request.form("disp_5")
response.redirect "delete.asp"
%>
完成了上面的程序,我们的新闻发布系统就可以在线添加和删除,设置显示等的功能来,需要加上搜寻功能,也可以在这个基础上加上。下一节我们来分析,如何;令新闻代码嵌入不同的网页,实现真正的在线新闻功能。
(三)把新闻代码插入你的页面
最好的新闻发布,当然是为网站本身定做的,那样才能与主页风格一致,但如此一来,新闻发布系统有缺乏了通用性了,不能移值到别的网站上使用,有得必有失,在这个基础上关键是找一个平行点。综合来考滤,最好的做法是与页面分离,那样就可以不影响网页的外观,而也能达到很好的效果,在使用新闻的网页上我们可以通过放置一条这样的script语句来调用新闻代码
<script language="javascript"
src="http//xxx.com.cn/special/linux/lnews/autoup.asp">
大家可以到这里看看效果
现在请大家看看autoup.asp的代码。
<% @language="vbscript" %>
<!--#include Virtual="/news/data/data.inc"-->
<%
Set rs = Server.CreateObject("ADODB.Recordset")
sql="select * from data Order by news_id Desc"
rs.open sql,conn,3,2
if rs.bof then
response.end
end if
dispmess="<table border=0 width=100% cellpadding=0>"
rs.movefirst
disp=""
count=0
disp="<tr><td width=100% ><b>广州市场</b></td></tr>"
do while not rs.eof
''广州市场
if Cint(rs("news_class"))=3 then
disp=disp&"<tr><td width=100% ><a href=http://www.aaaaa.com.cn/news/read.asp?news_id="&rs("news_id" target=_blank)&" target=_blank>>>"&rs("news_title")&"</a>(<font size=1>"&rs("news_year")&"-"&rs("news_month")&"-"&rs("news_day")&" "&rs("news_time")&"</font>)</td></tr>"
if count>=10 then ''clng(application("disp_1"))-1 then
exit do
end if
count=count+1
end if
rs.movenext
loop
disp=disp&"<tr><td width=100% align=right>【更多新闻...】 </td></tr>"
dispmess=dispmess&disp
rs.movefirst
disp=""
count=0
disp="<tr><td width=100% ><b>市场风云</b></td></tr>"
do while not rs.eof
''市场风云
if Cint(rs("news_class"))=1 then
disp=disp&"<tr><td width=100% ><a href=http://www.aaaaa.com.cn/news/read.asp?news_id="&rs("news_id" target=_blank)&" target=_blank>>>"&rs("news_title")&"</a>(<font size=1>"&rs("news_year")&"-"&rs("news_month")&"-"&rs("news_day")&" "&rs("news_time")&"</font>)</td></tr>"
if count>=10 then ''clng(application("disp_2"))-1 then
exit do
end if
count=count+1
end if
rs.movenext
loop
disp=disp&"<tr><td width=100% align=right>【更多新闻...】 </td></tr>"
dispmess=dispmess&disp
rs.movefirst
disp=""
count=0
disp="<tr><td width=100% ><b>IT新闻</b></td></tr>"
do while not rs.eof
''IT新闻
if Cint(rs("news_class"))=2 then
disp=disp&"<tr><td width=100% ><a href=http://www.aaaaa.com.cn/news/read.asp?news_id="&rs("news_id" target=_blank)&" target=_blank>>>"&rs("news_title")&"</a>(<font size=1>"&rs("news_year")&"-"&rs("news_month")&"-"&rs("news_day")&" "&rs("news_time")&"</font>)</td></tr>"
if count>=50 then ''clng(application("disp_3"))-1 then
exit do
end if
count=count+1
end if
rs.movenext
loop
disp=disp&"<tr><td width=100% align=right>【更多新闻...】 </td></tr>"
dispmess=dispmess&disp
dispmess=dispmess&"</table>"
dispmess="document.write(''"&dispmess&"'')"
%>
<%=dispmess%>
通过定制这段代码,就能制作出符合您要求的网页显示格式。
添加,删除,显示都讲解完了,最后一项,就是把新闻的详细内容显示出来。
当用户点击新闻标题,就打开disp.asp文件,同时使用GET这种方式来把参数传递过去,我们看看disp.asp的文件内容
<% @language="vbscript" %>
<!--#include Virtual="/news/data/data.inc"-->
<%
Set rs = Server.CreateObject("ADODB.Recordset")
sql="select * from data where Cstr(news_id)=''"&Cstr(request.querystring("news_id"))&"''"
rs.open sql,conn,3,2
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body topmargin="8" leftmargin="8">
<div align="center"><center>
<table border="0" width="600" cellpadding="0" cellspacing="0">
<tr>
<td width="100%"></td>
</tr>
<tr>
<td width="100%" bgcolor="#E6E6E6"><table border="0" width="100%" cellpadding="0">
<tr>
<td width="100%" bgcolor="#D2E8FF"><p align="center"><b><%=rs("news_title")%></b></td>
</tr>
<tr>
<td width="100%" bgcolor="#FBFDFF"><table border="0" width="100%" cellpadding="0">
<tr>
<td width="100%"><p align="right">(update:<%=rs("news_year")%>-<%=rs("news_month")%>-<%=rs("news_day")%> <%=rs("news_time")%>) </td>
</tr>
<tr>
<td width="100%"><p align="center"><%
if rs("news_pic")<>"" then
%> <img src="<%=rs("news_pic")%>"> <%
end if
%> </td>
</tr>
<tr>
<td width="100%"><div align="center"><center><table border="0" width="90%" cellpadding="0">
<tr>
<td width="100%"><%=rs("news_comment")%>
</td>
</tr>
</table>
</center></div></td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="100%" bgcolor="#D2E8FF"><p align="center"><a href="javascript:window.close( target=_blank)">关闭窗口</td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="100%"></td>
</tr>
<tr>
<td width="100%"></td>
</tr>
</table>
</center></div></a>
</body>
</html>
至此,新闻发布系统已能投入使用,试试看,是不是很方便,很容易就能增添每天的新闻内容了。下一节。我们将为新闻发布系统加入(标题/全文)检索功能,把它制作得更为完美

浙公网安备 33010602011771号