Lorenzo Pascucci
a- a+

Realizzare un Forum (4)

Come detto alla fine della precedente lezione, in questalezione parleremo delle pagine interne. Ovvero quelle pagineche ci permetto di vedere l'interno di ogni forum e dileggere ogni singolo messaggio (o meglio discussione) dellostesso forum.

Per fare questo abbiamo bisogno semplicemente di 2 pagine,che saranno poi il cuore del forum :
- forum.asp (visualizza tutti i msg di un forum)
- topic.asp (visualizza un msg ed eventuali risposte)

FORUM.asp

Questo è il codice, che commenteremo alla fine :

<!-- File di configurazione del forum --><!--#INCLUDE FILE="config.inc" --><%nDays = Request.Cookies("NumDays")' lettura cookieif Request.form("cookie") = "true" thenResponse.Cookies("NumDays") = Request.Form("days")Response.Cookies("NumDays").expires = date + 365nDays = Request.Form("Days")End Ifif nDays = "" thennDays = 30End IfdefDate = dateadd("d" , -cint(nDays), date)' Apertura connesione databaseset my_conn= Server.CreateObject("ADODB.Connection")my_Conn.Open ConnString' Cerca tutti i msg del forumstrSql ="SELECT Topics.T_Status, Topics.Forum_ID, Topics.Topic_ID, Topics.T_subject,Topics.T_Mail, Topics.T_Originator, Topics.T_Replies, Topics.T_Last_Post, Members.M_Name "strSql = strSql & "FROM Members INNER JOIN Topics ON Members.Member_id = Topics.T_Originator "strSql = strSql & "where Topics.Forum_ID = " & Request.QueryString("forum_id") & "and T_Last_Post >#" & defDate & "# order by Topics.T_Last_Post DESC"set rs = my_conn.Execute (StrSql)'Visualizza le nuove iconeif datediff("s" , session("last_here_date"), dt) > 1 thenisNew = "<img src='new_t.gif' alt='New Topic' border=0>" ElseisNew = "<img src='old_t.gif' alt='' border=0>" End IfEnd Function%><!-- Il file che include logo, motore di ricerca, e ricerca dei msg in base a n giorni --><!--#INCLUDE FILE="top.inc" --><font face="<% =DefaultFontFace %>" size="3"><br><a href="default.asp">Forum</a> | <a href="forum.asp?forum_id=<%= Request.QueryString("forum_id") %>&forum_title=_<%=server.Urlencode(Request.QueryString("forum_title"))%>"><%=Request.QueryString("forum_title")%></a>&nbsp;&nbsp;&nbsp;&nbsp;<small>Msg degli utilimi <%= ndays %> giorni</small><br><center><a href="post.asp?forum_id=<%=Request.QueryString("forum_id")%>&method=Topic&forum_title=_<%=server.Urlencode(Request.QueryString("forum_title"))%>">Invia nuovo messaggio</center></a><table border="0" width="95%" cellspacing="2" cellpadding="0"><tr><td align="center" bgcolor="<% =HeadCellColor %>">&nbsp;</td><td align="center" bgcolor="<% =HeadCellColor %>"><strong><font face="<% =DefaultFontFace %>" size="2" color="<% =HeadFontColor %>">Messaggio</font></strong></td><td align="center" bgcolor="<% =HeadCellColor %>"><strong><font face="<% =DefaultFontFace %>" size="2" color="<% =HeadFontColor %>">Autore</font></strong></td><td align="center" bgcolor="<% =HeadCellColor %>"><strong><font face="<% =DefaultFontFace %>" size="2" color="<% =HeadFontColor %>">Risposte</font></strong></td><td align="center" bgcolor="<% =HeadCellColor %>"><strong><font face="<% =DefaultFontFace %>" size="2" color="<% =HeadFontColor %>">Inviato</font></strong></td></tr><% If rs.Eof or rs.Bof then ' Nessun msg nel dbResponse.Write "<tr><td collspan=5>Non ci sono messaggi in questo forum</td></tr>"Elsedo until rs.Eof '## Display ForumResponse.Write "<tr>"if rs("T_Status") <> false thenResponse.Write "<td bgcolor='" & ForumCellColor & "' align='center'><a href='topic.asp?topic_id=" & rs("Topic_ID") & "&forum_id=" & Request.QueryString("forum_id") & "&Topic_Title=" & left(server.URLEncode(rs("T_Subject")), 50) & "&forum_title=" & server.URLEncode(Request.QueryString("forum_title")) & "&M=" & rs("T_Mail") & "'>" & isNew(rs("T_Last_Post")) & "</a></td>" & vbcrlfElse Response.Write "<td bgcolor='" & ForumCellColor & "' align='center'><a href='topic.asp?topic_id=" & rs("Topic_ID") & "&forum_id=" & Request.QueryString("forum_id") & "&Topic_Title=" & left(server.URLEncode(rs("T_Subject")), 50) & "&forum_title=" & server.URLEncode(Request.QueryString("forum_title")) & "&M=" & rs("T_Mail") & "&S=" & rs("T_Status") & "'><img src='no_t.gif' alt='Msg chiuso' border=0></a></td>" & vbcrlfEnd ifResponse.Write "<td bgcolor='" & ForumCellColor & "'><font face='" & DefaultFontFace & "' size='2'><a href='topic.asp?topic_id=" & rs("Topic_ID") & "&forum_id=" & Request.QueryString("forum_id") & "&Topic_Title=" & left(server.URLEncode(rs("T_Subject")), 50) & "&forum_title=" & server.URLEncode(Request.QueryString("forum_title")) & "&M=" & rs("T_Mail") & "&S=" & rs("T_Status") & "'>"Response.Write left(rs("T_Subject"), 50) & "</a>&nbsp;</font></td>"Response.Write "<td bgcolor='" & ForumCellColor & "' valign='top' align='center'><font face='" & DefaultFontFace & "' color='" & ForumFontColor & "' size='2'>" & rs("M_Name") & "</font></td>"Response.Write "<td bgcolor='" & ForumCellColor & "' valign='top' align='center'><font face='" & DefaultFontFace & "' color='" & ForumFontColor & "' size='2'>" & rs("T_Replies") & "</font></td>"Response.Write "<td bgcolor='" & ForumCellColor & "' valign='top' align='center'><font face='" & DefaultFontFace & "' color='" & ForumFontColor & "' size='1'>" & rs("T_Last_Post") & "</font></td>"Response.Write "</tr>"rs.MoveNextloopEnd If%></table> <p align="center"> <font face="<% =DefaultFontFace %>" size="3"><a href="default.asp">Visualizza tutti i forum</a> | <a href="post.asp?forum_id=<%=Request.QueryString("forum_id")%>&method=Topic&forum_title=_<%=server.Urlencode(Request.QueryString("forum_title"))%>">Invia nuovo messaggio</a></p> </body></html><%my_conn.Closeset my_conn = nothing%>

 

Scaricaquesto file (forum.asp)

Questo file, richiama come abbiamo visto (anche dai commentiinterni al codice) i soliti due file esterni Config.asp eTop.asp che abbiamo analizzato precedentemente. Il compito diquesta pagina è di prelevare dal forum prescelto tuttii messaggi inviati negli ultimi n giorni (specificati dalpiccolo modulo in alto). Visualizzerà quindi :oggetto, autore, il n di risposte e il giorno dell'invioper ogni messaggio. Sono inoltre presenti i link pervisualizzare nuovamente tutti i forum e per inserire un nuovomessaggio.
E' inclusa anche l'opzione di evidenziazione(attraverso diversa icona) dei messaggi nuovi e di quellivecchi rispetto l'ultima visita (tramite un cookie).

TOPIC.asp

Ecco il codice che analizzeremo in fondo:

l<!--#INCLUDE FILE="config.inc" --><%'Apertura connessione al dbset my_conn= Server.CreateObject("ADODB.Connection")my_Conn.Open ConnStringmypage=request("whichpage")If mypage="" thenmypage=1end ifmypagesize=request("pagesize")If mypagesize="" thenmypagesize=15end ifmySQL=request("strSQL")IF mySQL="" THENmySQL=SQLtempEND IFFunction FormatStr(String)on Error resume nextString = Replace(String, CHR(13), "")String = Replace(String, CHR(10) & CHR(10), "</P><P>")String = Replace(String, CHR(10), "<BR>")FormatStr = StringEnd FunctionFunction DoDropDown(tblName, DispField, ValueField, SelVal, name)StrSQL = "SELECT " & DispField & " , " & ValueField StrSQl = StrSQL & " FROM " & tblNamersdrop.Open strSQL, my_ConnResponse.Write "<Select Name='" & name & "'>"if rsdrop.EOF or rsdrop.BOF then Response.Write "<Option>Non trovato</option>" & vbcrlfElsedo until rsdrop.EOFif rs(ValueField) = cint(SelVal) thenResponse.Write "<option value='" & rsdrop(ValueField) & "' Selected>"Response.Write rsdrop(DispField) & "</option>" & vbcrlfElseResponse.Write "<option value='" & rsdrop(ValueField) & "'>"Response.Write rsdrop(DispField) & "</option>" & vbcrlfEnd ifrsdrop.MoveNextloopEnd ifResponse.Write "</select>" & vbcrlfrsdrop.Closeset rsdrop = nothing End FunctionSub GetFirst'# Get Origional PostingstrSql = "SELECT Members.M_Name,Members.M_ICQ, Members.Member_id, Topics.T_date, Topics.T_subject, Topics.T_Message, Topics.T_Originator, Topics.Topic_ID "strSql = strSql & "FROM Members INNER JOIN Topics ON Members.Member_id = Topics.T_Originator "strSql = strSql & "where topics.topic_id = " & Request.QueryString("topic_id") set rs = my_conn.Execute (strSql)If rs.Eof or rs.Bof then ' Nessun msg trovatoResponse.Write "<tr><td collspan=5>Nessun messaggio trovato</td></tr>"ElseResponse.Write "<tr>"Response.Write "<td bgcolor='" & ForumCellColor & "' valign=""top""><font color='" & ForumFontColor & "' face='" & DefaultFontFace & "' size='2'>" & rs("M_name") & "</font></td>"Response.Write "<td bgcolor='" & ForumCellColor & "' valign='top' ><font color='" & ForumFontColor & "' face='" & DefaultFontFace & "' size='1'>Posted - " & day(rs("T_Date")) & " " & monthname(month(rs("T_Date"))) & " " & year(rs("T_Date")) & " " & hour(rs("T_Date")) & ":" & Minute(rs("T_Date")) & "</font>"Response.Write "&nbsp;&nbsp;<a href=""Javascript:openWindow('profile.asp?mode=display&id=" & rs("member_id") & "')""><img src='profile.gif' alt='Show Profile' border=0 align='absmiddle' hspace=6></a>"Response.Write "&nbsp;&nbsp;<a href='mail.asp?id=" & rs("member_id") & "'><img src='email.gif' alt='Email Poster' border=0 align='absmiddle' hspace=6></a>"Response.Write "&nbsp;&nbsp;<a href='post.asp?method=editTopic&reply_id=" & Rs("Topic_ID") & "&auth=" & rs("T_Originator") & "&forum_title=" & server.URLEncode(Request.QueryString("forum_title")) & "&topic_title=" & server.Urlencode(Request.QueryString("topic_title")) & "&forum_id=" & Request.QueryString("forum_id") & "&topic_id=" & Request.QueryString("topic_id") &"'><img src='edit.gif' alt='Edit Message' border=0 align='absmiddle' hspace=6></a>"if ICQ = "true" then if trim(rs("M_ICQ")) <> "" thenResponse.Write "&nbsp;&nbsp;<a href=""Javascript:openWindow('ICQ.asp?ICQ=" & rs("m_ICQ") & "')""><img src='http://online.mirabilis.com/scripts/online.dll?icq=" & rs("M_ICQ") & "&img=5' width= 18 height=18 border=0 align=absmiddle hspace=6></a>"end ifend ifResponse.Write "<hr noshade size=1><font color='" & ForumFontColor & "' face='" & DefaultFontFace & "' size='2'>" & formatStr(rs("T_Message")) & "</font></td>"Response.Write "</tr></TD></TR>"End Ifset rs = nothingEnd Sub%><!--#INCLUDE FILE="top.inc" --><table border=0 width=100%><tr><td width=33% align=left><font face="<% =DefaultFontFace %>" size="2"><a href="default.asp">Forum</a> | <a href="forum.asp?forum_id=<%= Request.QueryString("Forum_id") %>&forum_title=_<%=server.Urlencode(Request.QueryString("forum_title"))%>"><%=Request.QueryString("forum_title")%></a> | <%=Request.QueryString("topic_title")%></td><td align=center width=33%><% if Request.QueryString("S") <> "False" then %><a href="post.asp?forum_id=<%= Request.QueryString("forum_id") %>&method=reply&forum_title=_<%=server.URLEncode(Request.QueryString("forum_title"))%>&topic_id=<%=Request.QueryString("topic_id")%>&topic_title=<%=server.URLEncode(Request.QueryString("topic_title"))%>&M=<%=Request.QueryString("M")%>"><font face="<% =DefaultFontFace %>" size="2">Post Reply</a> <% Else%>MESSAGGIO CHIUSO<% end if %></td><td align=right width=33%><a href="Javascript:openWindow('post_Page.asp?page=http://<%=Request.ServerVariables("HTTP_HOST")&Request.ServerVariables("URL")&"?"&Request.QueryString%>')"><font face="<% =DefaultFontFace %>" size="2">Invia ad un amico!</a></td></tr></table><table border="0" width="95%" cellspacing="2" cellpadding="3"><tr><td align="center" bgcolor="<% =HeadCellColor %>"><strong><font face="<% =DefaultFontFace %>" size="2" color="<% =HeadFontColor %>">Author</font></strong></td><td align="center" bgcolor="<% =HeadCellColor %>"><strong><font face="<% =DefaultFontFace %>" size="2" color="<% =HeadFontColor %>">Topic</font></strong></td></tr><% if mypage = 1 then Call GetFirst %><% '## Get all topicsFrom DBstrSql ="SELECT Members.M_Name, Members.M_ICQ, Reply.Reply_ID, Reply.R_Posted_By, Reply.Topic_ID, Reply.R_Message, Reply.R_Posted "strSql = strSQl & "FROM Members INNER JOIN Reply ON Members.Member_id = Reply.R_Posted_By "strSql = strSQl & "where topic_id = " & Request.QueryString("topic_id") & " order by reply.R_Posted"set rs = Server.CreateObject("ADODB.Recordset")rs.cachesize=15rs.open strSQL, my_conn, 3i = 0 If rs.Eof or rs.Bof then ' No categories found in DBResponse.Write ""Elsers.movefirstrs.pagesize=mypagesizemaxpages=cint(rs.pagecount)maxrecs=cint(rs.pagesize)rs.absolutepage=mypagehowmanyrecs=0rec = 1do until rs.Eof or rec = 16 '## Display Forumif i = 0 then CColor = AltForumCellColorelseCColor = ForumCellColorEnd ifResponse.Write "<tr>"Response.Write "<td bgcolor='" & CColor & "' valign=""top""><font color='" & ForumFontColor & "' face='" & DefaultFontFace & "' size='2'>" & rs("M_name") & "</font></td>"Response.Write "<td bgcolor='" & CColor & "' valign='top' ><font color='" & ForumFontColor & "' face='" & DefaultFontFace & "' size='1'>Posted - " & day(rs("R_Posted")) & " " & monthname(month(rs("R_Posted"))) & " " & year(rs("R_Posted")) & " " & hour(rs("R_Posted")) & ":" & Minute(rs("R_Posted")) & "</font>" Response.Write "&nbsp;&nbsp;<a href=""Javascript:openWindow('profile.asp?mode=display&id=" & rs("R_posted_by") & "')""><img src='profile.gif' alt='Show Profile' border=0 align='absmiddle' hspace=6></a>"Response.Write "&nbsp;&nbsp;<a href='mail.asp?id=" & rs("R_posted_by") & "'><img src='email.gif' alt='Email Poster' border=0 align='absmiddle' hspace=6></a>"Response.Write "&nbsp;&nbsp;<a href='post.asp?method=edit&reply_id=" & Rs("Reply_ID") & "&auth=" & server.URLEncode(rs("R_posted_by")) & "&forum_title=" & server.URLEncode(Request.QueryString("forum_title")) & "&topic_title=" & server.Urlencode(Request.QueryString("topic_title")) & "&forum_id=" & Request.QueryString("forum_id") & "&topic_id=" & Request.QueryString("topic_id") &"'><img src='edit.gif' alt='Edit Message' border=0 align='absmiddle' hspace=6></a>"if ICQ = "true" then if trim(rs("M_ICQ")) <> "" thenResponse.Write "&nbsp;&nbsp;<a href=""Javascript:openWindow('ICQ.asp?ICQ=" & rs("m_ICQ") & "')""><img src='http://online.mirabilis.com/scripts/online.dll?icq=" & rs("M_ICQ") & "&img=5' width= 18 height=18 border=0 align=absmiddle hspace=6></a>"end ifend if Response.Write "<hr noshade size=1><font color='" & ForumFontColor & "' face='" & DefaultFontFace & "' size='2'>" & formatStr(rs("R_Message")) & "</font></td>"Response.Write "</tr>"rs.MoveNexti = i + 1if i = 2 then i = 0rec = rec + 1loopEnd If%></table> <div align=left><font face="<% =DefaultFontFace %>" size="2"><%if maxpages > 1 thenif Request.QueryString("whichpage") = "" thenpge = 1elsepge = Request.QueryString("whichpage")end ifpad=" "scriptname=request.servervariables("script_name")Response.Write "Topic is " & maxpages & " Pages Long: &nbsp;&nbsp; " for counter=1 to maxpagesIf counter>=15 thenpad=""end ifif counter <> cint(pge) then ref="<a href='" & scriptname ref=ref & "?whichpage=" & counterref=ref & "&pagesize=" & mypagesize ref=ref & "&forum_title=" & server.URLEncode(Request.QueryString("forum_title")) ref=ref & "&topic_title=" & server.Urlencode(Request.QueryString("topic_title")) ref=ref & "&forum_id=" & Request.QueryString("forum_id") ref=ref & "&topic_id=" & Request.QueryString("topic_id") ref=ref & "'>" & pad & counter & "</a>"response.write ref & " "ElseResponse.Write counter & " "End ifif counter mod 15 = 0 thenresponse.write "<br>"end ifnextEnd if%></font></div> <p align="center"><font face="<% =DefaultFontFace %>" size="1">Click <a href="Javascript:openWindow('close.asp?topic_id=<%=Request.QueryString("topic_id")%>&topic_title=_<%=server.URLEncode(Request.QueryString("topic_title"))%>&forum_id=<%= Request.QueryString("Forum_id") %>')">Here</a> To Close Thread, Administrators & Moderators Only.<br><br><font face="<% =DefaultFontFace %>" size="3"><a href="default.asp">Show All Forums</a> | <% if Request.QueryString("S") <> "False" then %><a href="post.asp?forum_id=<%= Request.QueryString("forum_id") %>&method=reply&forum_title=<%=server.URLEncode(Request.QueryString("forum_title"))%>_&topic_id=<%=Request.QueryString("topic_id")%>&topic_title=<%=server.URLEncode(Request.QueryString("topic_title"))%>&M=<%=Request.QueryString("M")%>">Post Reply</a> <% Else%>Thread Closed<% end if %></body></html><%my_conn.Closeset my_conn = nothing%>

 

Scaricaquesto file (topic.asp)

Questo script, ha il compito di visualizzare il messaggiooriginale inviato e gli eventuali messaggi di risposta.Vediamo cosa fa. Per ogni messaggio indica : autore, giornodi invio, info sull'autore, email dell'autore edeventuale numero ICQ, naturalmente inserisce anche il corpodel messaggio e l'eventuale firma. Infine c'èla possibilità di inviare la pagina ad un amico e dichiudere i msg da parte dell' amministratore.

Nella prossima lezione, analizzeremo le pagine che ciserviranno per fare la ricerca dei messaggi, per inserire imessaggi, ecc...



Ti potrebbe interessare anche

commenta la notizia