top
Loading...
構建你的網站新聞自動發布系統之二

(二)添加和管理每天的新聞內容
當進行了新聞提交后,則交由一個名為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),"
")
tmpmess=replace(tmpmess,"《","《")
tmpmess=replace(tmpmess,"》","》")
tmpmess=replace(tmpmess,"
","
")
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"
%》

完成了上面的程序,我們的新聞發布系統就可以在線添加和刪除,設置顯示等的功能來,需要加上搜尋功能,也可以在這個基礎上加上。下一節我們來分析,如何;令新聞代碼嵌入不同的網頁,實現真正的在線新聞功能。

北斗有巢氏 有巢氏北斗