ASPcenter.net

25 febbraio 2010

Un Forum di discussione in ASP (Parte 4)

Archiviato in: Messaggistica — Tag:, , , — lukeonweb @ 19:58

Visualizzazione delle discussioni

Passiamo adesso alla realizzazione della pagina che mostra le discussioni presenti in una singola sezione, ovvero il file sezione.asp. Il procedimento è semplice: espletati tutti i controlli iniziali, già esaminati nelle pagine precedenti, passiamo alla lettura delle discussioni, verificando abbiano l’id passato in querystring alla pagina, ovvero l’id della sezione. Verifichiamo che ci siano discussioni nella sezione e ci regoliamo di conseguenza, ovvero lanciamo un messaggio che avvisa che non ci sono discussioni, oppure ordiniamo le discussioni per data.


Di seguito il codice:

<%@LANGUAGE = VBScript%>
<!--#include file="config.asp"-->
<%
Dim id, Cn, Mostra, Conta, Colore
id = Request.QueryString("id")
If id = "" Or IsNumeric(id) = False Or id < 1 Then
Response.Redirect "index.asp"
End If
If Not IsNumeric(id) Then
Cn.Close
Set Cn = Nothing
Response.Redirect "index.asp"
End If
Set Cn = Server.CreateObject("ADODB.Connection")
Cn.Open StrCn
Dim SQL
SQL = ""
SQL = SQL & "SELECT * FROM utenti INNER JOIN "
SQL = SQL & "(sezioni INNER JOIN messaggi ON "
SQL = SQL & "sezioni.id = messaggi.id_sezione) "
SQL = SQL & "ON utenti.id = messaggi.id_autore WHERE sezioni.id = "
SQL = SQL & id & " AND messaggi.titolo <> '' ORDER BY data DESC"
Set Mostra = Cn.Execute(SQL)
Conta = 0
%>
<html>
<head>
<title>Forum</title>
</head>
<body>

<!--#include file="menu.asp"-->

<table width="700" align="center" cellpadding="1" cellspacing="1"
border="1">
<tr bgcolor="#CCCCCC">
<td width="550">
<b>Elenco discussioni
<%
If Not Mostra.EOF Then
Response.Write("- " & Mostra("nome"))
End If
%><b>
</td>
<td width="150" align="center"><b>Autore<b></td>
</tr>
<%
If Mostra.EOF Then
With Response
.Write "<tr><td colspan=""2"">"
.Write "Nessuna discussione presente"
.Write "</td></tr>"
End With
Else While Not Mostra.EOF
Colore = "#FFFFFF"
If Conta Mod 2 <> 0 Then
Colore = "#EEEEEE"
End If
%>
<tr bgcolor="<%=Colore%>">
<td>
<a href="leggi.asp?id=<%=Mostra("id")%>"><%=Mostra("titolo")%></a>
</td>
<td align="center"><%=Mostra("username")%></td>
</tr>
<%
Conta = Conta + 1
Mostra.MoveNext
Wend
End If
%>
</table>

</body>
</html>
<%
Cn.Close
Set Cn = Nothing
%>

Lettura di un singolo messaggio

Passiamo adesso alla lettura di un singolo messaggio, utilizzando il file leggi.asp che, in funzione della variabili di tipo querystring id, ed in funzione dello schema del database affrontato nella prima parte dell’Articolo, legge un messaggio nuovo ed in coda, se presenti, mostra tutte le risposte alla discussione.

Di seguito il codice:

<%@LANGUAGE = VBScript%>
<!--#include file="config.asp"-->
<%
Dim id, Cn, DOMANDA, RISPOSTA
id = Request.QueryString("id")
If id = "" Or IsNumeric(id) = False Or id < 1 Then
Response.Redirect "index.asp"
End If
If Not IsNumeric(id) Then
Cn.Close
Set Cn = Nothing
Response.Redirect "index.asp"
End If
Set Cn = Server.CreateObject("ADODB.Connection")
Cn.Open StrCn
Dim SQL_D
SQL_D = ""
SQL_D = SQL_D & "SELECT * FROM messaggi, utenti "
SQL_D = SQL_D & "WHERE messaggi.id_autore = utenti.id "
SQL_D = SQL_D & "AND messaggi.id = " & id
Set DOMANDA = Cn.Execute(SQL_D)
Dim SQL_R
SQL_R = ""
SQL_R = SQL_R & "SELECT * FROM messaggi, utenti "
SQL_R = SQL_R & "WHERE messaggi.id_autore = utenti.id "
SQL_R = SQL_R & "AND messaggi.id_risposte = " & id
Set RISPOSTA = Cn.Execute(SQL_R)
%>
<html>
<head>
<title>Forum</title>
</head>
<body>

<!--#include file="menu.asp"-->

<p align="center">
<a href="sezione.asp?id=<%=id%>">Torna alla sezione</a>
</p>

<table width="700" align="center" cellpadding="1" cellspacing="1"
border="1">
<tr bgcolor="#CCCCCC">
<td><b><%=DOMANDA("titolo")%><b></td>
</tr>
<tr>
<td>
<i>A cura di <%=DOMANDA("username")%></b><br><br>
<%=DOMANDA("messaggio")%><br><br>
</td>
</tr>
<%
If RISPOSTA.EOF = False Then
While RISPOSTA.EOF = False
%>
<tr>
<td>
<i>A cura di <%=RISPOSTA("username")%></b><br><br>
<%=RISPOSTA("messaggio")%><br><br>
</td>
</tr>
<%
RISPOSTA.MoveNext
Wend
End If
%>
</table>

</body>
</html>
<%
Cn.Close
Set Cn = Nothing
%>

Nessun commento »

Non c'è ancora nessun commento.

RSS feed dei commenti a questo articolo. TrackBack URL

Lascia un commento

ASPcenter.net è il blog italiano su ASP, ASP.Net, VB.Net, C# e Visual Studio - Funziona con WordPress