|
|
|
|
|
Un articolo di: Matteo Gentile
del 04/08/2002
Letture: 8797
|
|
|
Buono sconto 4% su Toner e Cartucce agli utenti AZpoint. SU Iomiricarico.it!!
Creazione di un sondaggio
In questo esempio vedremo come creare e gestire un sempice sondaggio con
ASP. Questo esempio è basato su un database realizzato in MS Access, dal nome
Sonda.mdb, all'interno del quale è presente un'unica tabella, chiamata Sondaggio
con i seguenti campi:
- ID - contatore
- Voce - Testo 50
- Voti - Numerico (Intero Lungo)
La connessione
al database avviene con il metodo DSN-Less. L'esempio è strutturato su due
pagine e sfrutta l'Oggetto Session per
evitare che un utente possa votare più di una volta. Vediamo il codice della
prima pagina, Sondaggio.asp:
<html> <body> <% Session.Timeout=10 Dim
titolo, path titolo = "Come giudichi questo sito?"
' SOSTITUIRE QUI
IL PATH DEL VOSTRO DB path = "db/Sonda.mdb" Dim cn, rs, sql Set cn =
Server.CreateObject("ADODB.Connection") strConn="driver={Microsoft Access
Driver (*.mdb)};" strConn= strConn & "dbq=" & Server.MapPath(path)
cn.Open strConn sql = "SELECT * FROM Sondaggio" Set rs =
cn.Execute(sql) %> <font type=Times New Roman size=3><%=
titolo %></h3><center> <table width="60%" border="0"
cellspacing="0" cellpadding="2"> <% rs.MoveFirst do while not
rs.eof %> <tr> <td width=35%> <a
href=risultati.asp?action=vota&idx=<%= rs ("ID") %> target=_NEW>
<font face="Verdana, Arial" size="2"><b><%= rs ("VOCE")
%></b></font></a> </td> </tr> <%
rs.MoveNext loop rs.close Set rs=nothing cn.Close Set
cn=Nothing %> </table> </center> </body>
</html>
L'esempio di sondaggio proposto è quello del giudizio sul sito. La domanda
va inserita nella pagina, mentre le risposte con i relativi voti sono
memorizzate all'interno del DB. Vediamo ora il codice della seconda pagina,
Risultati.asp, che mostra i risultati del sondaggio:
<html> <body> <% Session.Timeout=10 Dim
titolo, path titolo = "Come giudichi questo sito?" path = "db/sonda.mdb"
Dim cn, rs, sql Set cn = Server.CreateObject("ADODB.Connection")
strConn="driver={Microsoft Access Driver (*.mdb)};" strConn= strConn
& "dbq=" & Server.MapPath(path) cn.Open strConn if
Request.QueryString("action") = "vota" then if Session("votato")<>1
then Dim index index = Request.QueryString("idx") sql = "SELECT VOTI
FROM Sondaggio WHERE ID=" &index Set rs = cn.Execute(sql) sql =
"UPDATE Sondaggio SET VOTI=" & rs("VOTI")+1 & " WHERE ID=" &index
cn.Execute(sql) Session("votato") = 1 end if end if sql =
"SELECT * FROM Sondaggio" Set rs = cn.Execute(sql) Dim voti_tot
voti_tot = 0 do while not rs.eof voti_tot = voti_tot + rs("VOTI")
rs.MoveNext loop %> <font type=Times New Roman
size=3><%= titolo %></h3><center> <table width="60%"
border="0" cellspacing="0" cellpadding="2"> <% rs.MoveFirst do
while not rs.eof Dim rate if voti_tot > 0 then rate =
Round(rs("VOTI")/voti_tot*100, 1) else rate = 0 end if %>
<tr> <td width=35%><font face="Verdana, Arial"
size="2"> <b><%= rs ("VOCE")
%></b></font></a></td> <td width="25">
<div align="right"><font face="Verdana, Arial"><b><font
size="1"><%= rate %>%</font></b></font></div>
</td> <td align=left> <hr size=10 width=<%=rate%>%
color=blue></td> </tr> <% rs.MoveNext loop
rs.close Set rs=nothing cn.Close Set cn=Nothing %>
<tr><td colspan=2 align=right> <b><font
face="Verdana, Arial, Helvetica, sans-serif" size="2"> <br>
<font size="1">Voti Totali: <%= voti_tot %>
</font></font></b></td> </table>
</center> </body> </html>
|
|
[Indietro]
[Su]
[Home]
[Commenti]
[V. Stampabile]
|
|
|
|
Commento di Anonimo (ip: 80.181.114.101), scritto il 21/11/2005 alle 00:47:06
|
|
Commento di Anonimo (ip: 82.106.83.203), scritto il 07/12/2005 alle 18:52:51
|
|
|
|