Ver Mensaje Individual
  #3 (permalink)  
Antiguo 29/06/2004, 10:08
svenson
 
Fecha de Ingreso: mayo-2003
Mensajes: 11
Antigüedad: 22 años
Puntos: 0
Ya esta, por fin me sale...

- Bueno, os comento el otro dia estaba mirando en google como actualizar varios checkbox de una BD de Access desde ASP y me encontre con esto:

http://www.thecodeproject.com/asp/ch...ordatabase.asp

- El tema es que no me acaba ni de funcionar ni de gustar. Asi que dije vamos a personalizalizarlo un poco y al final lo he conseguido hacer con unas modificaciones. En esa pagina lo hacen todo en una y yo lo he dividido en 3 pero bueno. Os pongo el codigo por si os vale para algo:

- Haceis una BD en Access llamada "base.mdb" y creais una tabla que se llame Noticias. Dentro de esa tabla creais 2 campos uno llamado "numero"(lo poneis como Autonumerico) y otro llamado "ocultar"(lo poneis como campo Si/No).

Primera pagina:

chkboxes.asp

----------------------------------------------------------

<%@ Language=VBScript %>
<%option explicit%>
<%Response.Buffer = false%>
<HTML>
<HEAD>
<title>
Check box updater
</title>
</HEAD>
<BODY>
<%
Dim Conexion,Tabla,x
Const adCmdText = &H0001
Const adOpenStatic = 3
Set Conexion=Server.CreateObject("adodb.connection")
Set Tabla=Server.CreateObject("adodb.recordset")
Conexion.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="&Server.MapPath("base.mdb")&";"
x="Select * from Noticias"
Tabla.Open x, Conexion,adopenstatic,adcmdtext
dim iNum
dim strAll
dim i
dim arrAll
dim arrVals
dim arrInactive()
dim bThere
dim IDX
dim strSql
dim z
iNum = Request.QueryString ("numBoxes")
if iNum = "" then
iNum = 10
end if
if Request.Form = "" then
strAll = ""
%>
<form name="form1" action="chkboxes2.asp" method="post">
<table>
<%
iNum=Tabla.RecordCount
i=Tabla.Fields("numero")
for i = 1 to Tabla.RecordCount
%>
<tr>
<td>Checkbox<%=i%>:</td>
<td><input type="checkbox" name="chkBox" value="<%=i%>" <%if Tabla("ocultar")=true then Response.write("checked") else Response.Write("") end if%>></td>
</tr>
<%
strAll = strAll & i & ","
Tabla.MoveNext
Next
%>
<tr>
<td colspan="2"></td>
</tr>
</table>
<%
'trim off the trailing ","
strAll = left(strAll, (len(strAll) -1))
%>
<input type="hidden" name="allBoxes" value="<%=strAll%>">
<%
end if
Tabla.Close
%>
<input type="submit" value="Salvar Cambios">
</form>
</BODY>
</HTML>


-----------------------------------------------------------

Segunda pagina:

chkboxes2.asp

<%@ Language=VBScript %>
<%option explicit%>
<%Response.Buffer = false%>
<HTML>
<HEAD>
<title>
Check box updater
</title>
</HEAD>
<form name="form1" action="chkboxes3.asp" method="post">
<BODY onload="javascript:form1.submit();">
<%
Dim Conexion,Tabla,x
Const adCmdText = &H0001
Const adOpenStatic = 3
Set Conexion=Server.CreateObject("adodb.connection")
Set Tabla=Server.CreateObject("adodb.recordset")
Conexion.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="&Server.MapPath("base.mdb")&";"
x="Select * from Noticias"
Tabla.Open x, Conexion,adopenstatic,adcmdtext
dim iNum
dim strAll
dim i
dim arrAll
dim arrVals
dim arrInactive()
dim bThere
dim IDX
dim strSql
dim z
iNum = Request.QueryString ("numBoxes")
if iNum = "" then
iNum = 10
end if
if Request.Form <> "" then
arrAll = split(Request.Form ("allBoxes"), ",")
arrVals = split(Request.Form ("chkBox"), ",")
strSql = ""
strSql = strSql & "UPDATE Noticias SET ocultar=true WHERE "
for i = 0 to ubound(arrVals)
if i = 0 then
strSql = strSql & "numero = "& arrVals(i)
else
strSql = strSql & " OR numero = "& arrVals(i)
end if
next
%>
<input type="Hidden" name="Uno" value="<%=strSql%>">
<%
redim arrInActive(ubound(arrAll) - ubound(arrVals))
IDX = 0
for i = 0 to ubound(arrAll)
bThere = false
for z = 0 to ubound(arrVals)
if trim(arrVals(z)) = trim(arrAll(i)) then
bThere = true
end if
next
if bThere = false then
arrInactive(IDX) = arrAll(i)
IDX = IDX + 1
end if
next
strSql = ""
strSql = strSql & "UPDATE Noticias SET ocultar=false WHERE "
for i = 0 to ubound(arrInactive)
if arrInactive(i) <> "" then
if i = 0 then
strSql = strSql & "numero ="& arrInactive(i)
else
strSql = strSql & " OR numero ="& arrInactive(i)
end if
end if
next
%>
<input type="Hidden" name="dos" value="<%=strSql%>">
<%
end if
Tabla.Close
%>
</form>
</BODY>
</HTML>

-----------------------------------------------------

Tercera pagina:

chkboxes3.asp

<meta http-equiv="Refresh" content="3;URL=chkBoxes.asp">
<%@ Language=VBScript %>
<%
Dim Conexion,Conexion2,Tabla,x,a,b,c,d
Set Conexion=Server.CreateObject("adodb.connection")
a="Update Noticias set ocultar=false"
b=Request("Uno")
c="UPDATE Noticias SET ocultar=true WHERE "
Conexion.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="&Server.MapPath("base.mdb")&";"
Conexion.Execute a
if b <> c then
Conexion.Execute b
end if
Conexion.Close
Response.write("<br>La consulta que va ha hacer es esta: "&"<br><br>"&b)
%>

------------------------------------------------------------------


- A este codigo le veo bastantes utilidades como ocultar/mostrar varias noticias(o registros) a la vez en una pagina(es la idea que tenia yo) desde una web, eliminar varias noticias(o registros) a la vez, .... Lo veo muy util.

- Gracias por las respuestas. Un saludo.