SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Adding Discussion Forums : User Pages - Viewing Forums

The entry page to the discussion forum is forums.asp. This page simply enumerates and outputs all the discussion forums with their descriptions. From this page, the user can select an individual forum taking them to the viewForum.asp.

Forums.asp

The script for forums.asp is very simple. Rather than execute a query, we merely open the t_forums table and output each record. In a production system, you may want to add ordering information to the table and apply a sort, or depending upon your layout needs, you may want to create an index page manually.

<%@ Language=VBScript %>
<% option explicit %>
<HTML>
<HEAD>
<!-- #include virtual="/inc/utility.asp" -->
</HEAD>
<BODY>

<%
  Dim oRS, oCmd, oConn
  Const ACTIONSAVE = "Save..."

  set oConn = GetConnection()

  Response.Write("<H2>" & HOMEPAGE & " : " & SITENAME & " Discussion Forums</H2>")
  Set oRS = GetTable(oConn,"t_forums")
  Response.Write("<UL>")
  while not oRS.eof
    Response.Write("<LI><A HREF=""viewForum.asp?forum_id=" & oRS.fields("forum_id") & """>")
    Response.write(oRS.fields("forum_name") & "</A>")
    if not isnull(oRS.fields("forum_update")) then
      ' Output date of last update if one exists
      Response.Write(" (last updated " & oRS.fields("forum_update") & ")")
    end if
    Response.Write("<BR>" & oRS.fields("forum_desc"))
    oRS.movenext
  wend
  Response.Write("</UL>")
%>
</BODY>
</HTML>

viewForum.asp

viewForum.asp is a much more interesting script. This page outputs the topics within a particular forum. To accomodate larger topic lists, this page also supports paging by displaying 20 topics at a time (this value can be changed).

Below we take you through the commented script.

<%@ Language=VBScript %>
<% option explicit 

Dim forumID, iStart

' The forum being viewed
forumID = Request.QueryString("forum_id")

' The start record
iStart  = Request.QueryString("start")

' Make sure forum and start records are numbers
if forumID="" or not isnumeric(forumid) then Response.redirect("forums.asp")
if not isnumeric(iStart) or iStart<0 then iStart=0

Dim oCmd, oRS

' Get the list of messages
Set oCmd = GetStoredProcedure(getConnection(),"sp_getTopicsForForum")
oCmd.Parameters.append oCmd.CreateParameter("forum_id", adInteger, adParamInput,10,forumID)
set oRS = oCmd.execute()

' Two recordsets are returned. The first recordset contains
' information about the forum. If no forum exists (bad id) then
' redirect back to the forum list
if oRS.eof then Response.Redirect("forums.asp")

' Get forum information
Dim sForumName, sForumDesc
sForumName = oRS.fields("forum_name")
sForumDesc = oRS.fields("forum_desc")

' Get next recordset - the list of topics
set oRS = oRS.nextRecordset


%>
<HTML>
<HEAD>
<!-- #include virtual="/inc/utility.asp" -->
<TITLE><%=sForumName%></TITLE>
</HEAD>
<BODY>

<%

' Output Header
Response.Write("<H2>" & HOMEPAGE & " : <A HREF=""forums.asp"">Discussion Forums</A> : " & sForumName & " Forum</H2><P>" & sForumDesc)

' This defines the number of records to display per page
const chunking = 20

Dim iCount 
iCount = 0

' A link to start a new topic (security check occurs on the starttopic.asp page)
Response.Write("<P><B><A HREF=""startTopic.asp?forum_id=" & forumID & """>Start New Topic</A></B>")

' If messages exist, move to the start record (for paging)
if not oRS.eof then oRS.move iStart

' If starting past first record, display previous page link
if iStart>0 then 
  Response.Write("<P><A HREF=""viewForum.asp?forum_id=" & forumID & "&start=" & (iStart - Chunking) & """>Previous Messages...</A>")
end if

' If no more records, display message
if oRS.eof then 
  Response.Write("<P><B><EM>No more messages</EM></B>")
end if

' Output each topic until no more topics or reach per page record limit
Response.Write("<UL>")
while not oRS.eof and iCount<chunking
  ' Topic Name
  Response.Write("<LI><A HREF=""viewtopic.asp?topic_id=" & oRS.fields("topic_id") & """>" & oRS.fields("topic_name") & "</A>")

  ' Topic Stats
  if (oRS.fields("topic_count")=0) then
    Response.Write(" (Started by " & oRS.fields("u_name") & " on " & oRS.fields("topic_create") & ")")
  else
    Response.Write(" (Started by " & oRS.fields("u_name") & ", Last reply on " & oRS.fields("topic_update") & " : " & oRS.fields("topic_count") & " Repl")
    if oRS.fields("topic_count")=1 then Response.write("y)") else Response.write("ies)")
  end if
 
  ' Count rows
  iCount = iCount + 1
  oRS.movenext
wend
Response.Write("</UL>")
	
' If more records, output link to next page
if not oRS.eof then
  Response.Write("<A HREF=""viewForum.asp?forum_id=" & forumID & "&start=" & (iCount + iStart) & """>More Messages...</A>")
end if
%>
</BODY>
</HTML>

On the next page, we continue by explaining how to create new topics and display the topics and replies.