|
|
|
Este sistema de encuestas es muy sencillo, lo creé especialmente para tener de 2 a 4 opciones de respuesta, pero vosotros lo podéis ampliar a más. Este script se puede ver funcionando a la perfección en www.ommadawn.es.vg. Además, tiene un sistema de adición de encuestas por formulario, para no tener que subir y bajar bases de datos. Los archivos que tenéis que crear son nuevaencuesta.asp , resultados.asp , verencuesta.asp , encuestas.mdb , nuevaencuesta.htm . Luego, a mayores, están las imágenes NEGRO.GIF, ROJO.GIF, VERDE.GIF, AZUL.GIF, que son unas imágenes de 6x6 píxeles de cada color, luego el sistema se encarga de ponerle su tamaño verdadero para mostrar el gráfico.
encuestas.mdb La base de datos que consta de los siguientes campos en la tabla encuestas:
id: autonumérico pregunta: texto respuesta1: numérico respuesta2: numérico respuesta3: numérico respuesta4: numérico votos1: numérico votos2: numérico votos3: numérico votos4: numérico fecha: Fecha/hora nresp: numérico
nuevaencuesta.asp Página que se encarga de añadir a la base de datos una nueva encuesta.
<% Response.Buffer = true dim cnn,rst ´ Creamos la conexión a la base de datos sin DSN set cnn = Server.CreateObject("ADODB.Connection") set rst = Server.CreateObject("ADODB.RecordSet") cnn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _ Server.MapPath("encuestas.mdb")) sqltext = "SELECT * FROM encuestas" rst.Open sqltext,cnn,3,3
fecha = date()
pregunta=Request.Form("pregunta") respuesta1=request.form("respuesta1") respuesta2=request.form("respuesta2") respuesta3=request.form("respuesta3") respuesta4=request.form("respuesta4") nresp=request.form("nresp")
rst.AddNew rst("pregunta") = pregunta rst("respuesta1") = respuesta1 rst("respuesta2") = respuesta2
´miramos que mínimo tenga dos respuestas, y si tiene tres y/o cuatro respuestas se añaden. if respuesta3="" then rst("respuesta3") = " " else rst("respuesta3") = respuesta3 end if
if respuesta4="" then rst("respuesta4") = " " else rst("respuesta4") = respuesta4 end if rst("votos1") = "0" rst("votos2") = "0" if respuesta3="" then rst("votos3") = "0" else rst("votos3")="0" end if
if respuesta4="" then rst("votos4") = "0" else rst("votos4")="0" end if
rst("nresp") = nresp rst("fecha") = fecha rst.update
response.write("La encuesta se ha insertado con éxito.") %>
resultados.asp Esta página se encarga de mostrar los resultados sin votar
<%Set con = Server.CreateObject("ADODB.Connection") Con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _ Server.MapPath("encuestas.mdb")) sql = "SELECT * FROM encuestas WHERE id=" & Request.querystring("id") & "" set result = Con.Execute(sql)
´hay que asegurarse cuantas respuestas tiene la pregunta, no vaya a ser que nos de unos errores. if result("nresp")=4 then
votos1=result("votos1") votos2=result("votos2") votos3=result("votos3") votos4=result("votos4") total=votos1+votos2+votos3+votos4
´y aquí también nos puede dar error si calculamos el porcentaje cuando todos los votos tienen 0 repsuestas (al no votar y crear la encuesta automáticamente, ver este archivo daría error). if votos1=0 and votos2=0 and votos3=0 and votos4=0 then por1a=0 por2a=0 por3a=0 por4a=0 else por1a=votos1/total*100 por2a=votos2/total*100 por3a=votos3/total*100 por4a=votos4/total*100 end if
´y ahora le acortamos el porcentaje a cuatro dígitos para que no sea molesto por1=left(por1a,4)&"%" por2=left(por2a,4)&"%" por3=left(por3a,4)&"%" por4=left(por4a,4)&"%"
elseif result("nresp")=3 then
votos1=result("votos1") votos2=result("votos2") votos3=result("votos3") total=votos1+votos2+votos3
if votos1=0 and votos2=0 and votos3=0 and votos4=0 then por1a=0 por2a=0 por3a=0 else por1a=votos1/total*100 por2a=votos2/total*100 por3a=votos3/total*100 end if
por1=left(por1a,4)&"%" por2=left(por2a,4)&"%" por3=left(por3a,4)&"%"
elseif result("nresp")=2 then
votos1=result("votos1") votos2=result("votos2") total=votos1+votos2
if votos1=0 and votos2=0 and votos3=0 and votos4=0 then por1a=0 por2a=0 else por1a=votos1/total*100 por2a=votos2/total*100 end if
por1=left(por1a,4)&"%" por2=left(por2a,4)&"%"
end if %><html><head> <title>Encuesta: <%=result("pregunta")%></title> <style> <!-- body, table, td { font-family: Arial; font-size: 8 pt } a { color:black; text-decoration:none; font-weight: bold } a:hover { color:white} --> </style> </head> <body bgcolor="#E9E9E9"> <center><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td width="100%" bgcolor="#C0C0C0"><div align="center"><table border="0" cellpadding="4" cellspacing="1" width="100%"> <tr><td width="100%" bgcolor="#FFFFFF">Encuesta: <b><%=result("pregunta")%></b><br>Fecha: <b><%=result("fecha")%></b><table border="0" cellpadding="2" cellspacing="2" width="100%"><tr><td width="40%" align="right"><%=result("respuesta1")%></td><td width="60%"><img src="gris.gif" width="<%=por1%>%" align=middle height="10"> <%=result("votos1")%> - <%=por1%></td></tr> <tr><td width="40%" align="right"><%=result("respuesta2")%></td><td width="60%"><img src="azul.gif" align=middle width="<%=por2%>%" height="10"> <%=result("votos2")%> - <%=por2%></td></tr> <%if result("nresp")=3 then%><tr><td width="40%" align="right"><%=result("respuesta3")%></td><td width="60%"><img src="verde.gif" align=middle width="<%=por3%>%" height="10"> <%=result("votos3")%> - <%=por3%></td></tr> <%elseif result("nresp")=4 then%><tr><td width="40%" align="right"><%=result("respuesta4")%></td><td width="60%"><img src="rojo.gif" align=middle width="<%=por4%>%" height="10"> <%=result("votos4")%> - <%=por4%></td></tr><%end if%> <tr><td width="40%" align="right"></td><td width="60%">Total: <b><%=total%></td></tr> </table> </div></td></tr><tr><td width="100%" bgcolor="#C0C0C0" align=center><a href="javascript:close()">Cerrar ventana</a></td></tr></table></div></td></tr></table></center></body></html>
verencuesta.asp Esta página se encarga de votar y mostrar los resultados con el voto ya dado.
<% ´conexion a la base Set oConn = Server.CreateObject("ADODB.Connection") oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="&Server.MapPath("encuestas.mdb")&";" strsql = "SELECT * FROM encuestas where id="&request.querystring("id")&""
Set rs = Server.CreateObject("ADODB.RecordSet") RS.open strsql, oConn, 3, 3
votos=Request.querystring("opcion")
´añadimos 1 a los votos rs(votos)=rs(votos)+1 rs.update
rs.Close set rs=nothing oConn.Close %><%
´conexión a la base de datos para mostrar el mensaje Set con = Server.CreateObject("ADODB.Connection") Con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _ Server.MapPath("encuestas.mdb")) sql = "SELECT * FROM encuestas WHERE id=" & Request.querystring("id") & "" set result = Con.Execute(sql)
if result("nresp")=4 then
votos1=result("votos1") votos2=result("votos2") votos3=result("votos3") votos4=result("votos4") total=votos1+votos2+votos3+votos4
por1a=votos1/total*100 por2a=votos2/total*100 por3a=votos3/total*100 por4a=votos4/total*100
por1=left(por1a,4)&"%" por2=left(por2a,4)&"%" por3=left(por3a,4)&"%" por4=left(por4a,4)&"%"
elseif result("nresp")=3 then
votos1=result("votos1") votos2=result("votos2") votos3=result("votos3") total=votos1+votos2+votos3
por1a=votos1/total*100 por2a=votos2/total*100 por3a=votos3/total*100
por1=left(por1a,4)&"%" por2=left(por2a,4)&"%" por3=left(por3a,4)&"%"
elseif result("nresp")=2 then
votos1=result("votos1") votos2=result("votos2") total=votos1+votos2
por1a=votos1/total*100 por2a=votos2/total*100
por1=left(por1a,4)&"%" por2=left(por2a,4)&"%"
end if %><html><head> <title>Encuesta: <%=result("pregunta")%></title> <style> <!-- body, table, td { font-family: Arial; font-size: 8 pt } a { color:black; text-decoration:none; font-weight: bold } a:hover { color:white} --> </style> </head> <body bgcolor="#E9E9E9" marginleft=0 margintop=0 topmargin=0 leftmargin=0> <center><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td width="100%" bgcolor="#C0C0C0"><div align="center"><table border="0" cellpadding="4" cellspacing="1" width="100%"> <tr><td width="100%" bgcolor="#FFFFFF">Encuesta: <b><%=result("pregunta")%></b><br>Fecha: <b><%=result("fecha")%></b><table border="0" cellpadding="2" cellspacing="2" width="100%"><tr><td width="40%" align="right"><%=result("respuesta1")%></td><td width="60%"><img src="gris.gif" width="<%=por1%>%" align=middle height="10"> <%=result("votos1")%> - <%=por1%></td></tr> <tr><td width="40%" align="right"><%=result("respuesta2")%></td><td width="60%"><img src="azul.gif" align=middle width="<%=por2%>%" height="10"> <%=result("votos2")%> - <%=por2%></td></tr> <%if result("nresp")=3 then%><tr><td width="40%" align="right"><%=result("respuesta3")%></td><td width="60%"><img src="verde.gif" align=middle width="<%=por3%>%" height="10"> <%=result("votos3")%> - <%=por3%></td></tr> <%elseif result("nresp")=4 then%><tr><td width="40%" align="right"><%=result("respuesta4")%></td><td width="60%"><img src="rojo.gif" align=middle width="<%=por4%>%" height="10"> <%=result("votos4")%> - <%=por4%></td></tr><%end if%> <tr><td width="40%" align="right"></td><td width="60%">Total: <b><%=total%></td></tr> </table> </div></td></tr><tr><td width="100%" bgcolor="#C0C0C0" align=center><a href="javascript:close()">Cerrar ventana</a></td></tr></table></div></td></tr></table></center></body></html>
nuevaencuesta.htm Formulario para añadir una nueva encuesta
<html><head><title>Nueva Encuesta</title> </head><body> <form action="nuevaencuesta.asp" method="Post"> Pregunta: <input type="text" size=30 name="pregunta"> <br>Respuesta 1: <input type="text" size=17 name=respuesta1> <br>Respuesta 2: <input type="text" size=17 name=respuesta2> <br>Respuesta 3: <input type="text" size=17 name=respuesta3> <br>Respuesta 4: <input type="text" size=17 name=respuesta4> <p>Número respuestas: <input type="text" name="nresp" size="20"><br> <input type="submit" value="enviar"> </p> </form></body></html>
Espero que este script os sea de utilidad para todos, que las encuestas son muy utilizadas. Saludos.
|
|