Monday, December 17, 2007

Discussion Board - How to create an index of the discussion?

Discussion Board - How to create an index of the discussion?

Step 1 – We need to list out all the discussions in a table:

<%

Set conn = Server.CreateObject("Adodb.Connection")

conn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("db1.mdb")

Set rs = Server.CreateObject("Adodb.Recordset")

rs.open "Discuss",conn,1,3

do while not rs.eof

response.write("

" & _

"

" & _

"

" & _

"

" & _

"

")

rs.movenext

loop

rs.close

set rs = nothing

set conn = nothing

%>

User Title Post Date
" & rs.fields("User") & "" & rs.fields("Title") & "" & rs.fields("PostDate") & "


Step 2 – We need to create a link that will show the details of the discussion.

In the HTML code, we need to create a hyperlink. So we need to use this command:

Now, the problem is, you don’t know How many links you need to create. So you need to use the ASP script!

First, we can use the Looping to create all the links:

<% For I = 1 to 10 %>

<%Next%>

But, this example only can generate 10 links to show the same file! So How can we let the user to select the different discussion titles and show the contents?

First, we understand that each discussion has its own ID, so we can use the ID to identify it.

Now, we can try to create a link like this:

‘1 is the ID of the Discuss


To create a dynamic one:


<%=rs.fields("ID")%>’> ‘We can get the ID from the database

The complete example is shown below:

<%

Set conn = Server.CreateObject("Adodb.Connection")

conn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("db1.mdb")

Set rs = Server.CreateObject("Adodb.Recordset")

rs.open "Discuss",conn,1,3

do while not rs.eof

response.write("

" & _

"

" & _

"

" & _

"

" & _

"

")

rs.movenext

loop

rs.close

set rs = nothing

set conn = nothing

%>

User Title Post Date
" & rs.fields("User") & "" & rs.fields("Title") & "" & rs.fields("PostDate") & "



No comments: