Suppongo che tu abbia 2 tabelle:
tabellaUtenti
idUtente - contatore (chiave primaria)
nomeUtente - testo
...
tabellaOggetti
idOggetto - contatore (chiave primaria)
nomeOggetto - testo
...
A te serve una terza tabella dove mettere in relazione utenti ed oggetti.
tabellaRelazioniUtentiOggetti (senza alcuna chiave primaria)
idUtente - numerico
idOggetto - numerico
A questo punto la tabella HTML la costruisci facendo una prima query sulla tabella utenti ed elencandoli tutti in una riga <tr> ognuno dentro la propria cella.
Inoltre, durante il ciclo di lettura della tabella utenti, memorizzo in ordine di lettura gli idUtenti dentro ad un array.
Successivamente leggo la tabella oggetti che metterò ognuno dentro alla prima cella di ogni riga, mentre nelle celle successive della stessa riga metterò i campi checkbox con un ciclo che legge l'array degli idUtenti (precedentemente creato) in modo da dare ad ogni checkbox il name="id_valoreIDOggetto_valoreIDUtente". Con una funzione apposita ottengo se il campo deve essere checkato di default o meno (verifico se nella tabella delle relazioni ci sta un record con idOggetto e idUtente).
Infine, il submit di questa form invierà tutti i checkbox alla pagina successiva la quale, per prima cosa fa un "DELETE * FROM tabellaRelazioniUtentiOggetti" e poi, con un ciclo sulla collection request.form intercetto i campi che iniziano per "id_", verifico che contengano 1 come valore (ovvero che siano stati spuntati) e se lo sono, splitto il name del campo in base al carattere _ e prendo gli elementi in posizione 1 e 2, rispettivamente idOggetto e idUtente e faccio una INSERT INTO nella tabellaRelazioniOggettiUtenti.
codice:
<%
sqlServer_name = "localhost"
sqlServer_db = "samples"
sqlServer_login = "xxxxxx"
sqlServer_password = "yyyyyy"
connString = "Provider=sqloledb;" &_
"Network Library=DBMSSOCN;" &_
"Encrypt=yes;" &_
"Data Source=" & sqlServer_name & ";" &_
"Initial Catalog=" & sqlServer_db & ";" &_
"User Id=" & sqlServer_login &";" &_
"Password=" & sqlServer_password & ";"
thisPage = request.serverVariables("PATH_INFO")
submit = request.serverVariables("REQUEST_METHOD") = "POST"
reloadPage = false
function verifyRelation(theConn,theObjectID,theUserID)
theSQL = "SELECT COUNT(*) FROM tabellaRelazioniOggettiUtenti WHERE idOggetto = " & theObjectID & " AND idUtente = " & theUserID
set theRS = theConn.execute(theSQL)
verifyRelation = theRS(0)
theRS.close
set theRS = nothing
end function
dim arrayID()
set conn = server.createObject("ADODB.Connection")
conn.open connString
if submit then
conn.execute("DELETE FROM tabellaRelazioniOggettiUtenti")
for each item in request.form
if left(item,3) = "id_" and request.form(item) = "1" then
arrayItems = split(item,"_")
objectID = arrayItems(1)
userID = arrayItems(2)
conn.execute("INSERT INTO tabellaRelazioniOggettiUtenti (idOggetto, idUtente) VALUES(" & objectID & ", " & userID & ")")
end if
next
reloadPage = true
end if
sql = "SELECT idUtente, nomeUtente FROM tabellaUtenti ORDER BY idUtente"
set rs = server.createObject("ADODB.Recordset")
rs.open sql, conn, 1, 3
response.write "<form method=""post"" action=""" & thisPage & """>" & vbCrLf
response.write " <table border=""1"">" & vbCrLf
response.write " <tr>" & vbCrLf
response.write " <td></td>" & vbCrLf
if not rs.eof then
i = 0
totUsers = rs.recordCount
reDim arrayID(totUsers - 1)
do until rs.eof
response.write " <td>" & rs("nomeUtente") & "</td>" & vbCrLf
arrayID(i) = rs("idUtente")
rs.moveNext
i = i + 1
loop
end if
response.write " </tr>" & vbCrLf
rs.close
sql = "SELECT idOggetto, nomeOggetto FROM tabellaOggetti ORDER BY idOggetto"
rs.open sql, conn, 1, 3
if not rs.eof then
do until rs.eof
response.write " <tr>" & vbCrLf
response.write " <td>" & rs("nomeOggetto") & "</td>" & vbCrLf
for i = 0 to uBound(arrayID)
response.write " <td><input type=""checkbox"" name=""id_" & rs("idOggetto") & "_" & arrayID(i) & """ value=""1"""
if verifyRelation(conn,rs("idOggetto"),arrayID(i)) = 1 then
response.write "checked=""checked"""
end if
response.write " /></td>" & vbCrLf
next
response.write " </tr>" & vbCrLf
rs.moveNext
loop
end if
rs.close
set rs = nothing
response.write " <tr>" & vbCrLf
response.write " <td colspan=""" & totUsers + 1 & """><input type=""submit"" value=""esegui"" /></td>" & vbCrLf
response.write " </tr>" & vbCrLf
response.write " </table>" & vbCrLf
response.write "</form>" & vbCrLf
conn.close
set conn = nothing
if reloadPage then
response.write "<script type=""text/javascript"">" & vbCrLf
response.write " document.location.href='" & thisPage & "';" & vbCrLf
response.write "</script>" & vbCrLf
end if
%>