Ecco. Non spaventarti, è un motore di ricerca e perciò è complesso,
ma tu recupera solo le istruzioni relative alla paginazione (poche righe
verso l'inizio e poi la tabellina alla fine che crea i link pagina avanti... pagina indietro... e vai a pagina...), è la stessa che
useresti in un file più semplice.
codice:
<html>
<head>
<title>Ricerca per parole chiave</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
#piedepagina{
position: relative;
left: 20px;
bottom: 25px;
}
-->
</style>
</head>
<body>
<%
call open_connection()
checked1 = request("campo1")
checked2 = request("campo2")
checked3 = request("campo3")
checked4 = request("campo4")
checked5 = request("campo5")
'esempio di ciclo al posto della serie di istruzioni sopra (nb: checked(i) diventa un array!):
'dim checked(5)
'for i=1 to 5
'checked(i) = request("campo"&i)
'next
parole=trim(request("parole"))
parole=lcase(parole)
if len(parole) > 0 then
Set rs = Server.CreateObject("ADODB.Recordset")
'xxxxxxxxxxxxxx estrazione records contenenti una o più parole chiave (indipendentemente da maiuscole/minuscole)xxxxxxxxxx
'nb: 1) con = cerca solo le stringhe, con "like" cerca anche le sottostringhe
' 2) diverso comportamento con "or" oppure "and"
' 3) vedi anche NB subito qui sotto!
'NB: un'altra possibilità, anzichè splittare l'input, è:
'sql = "SELECT * FROM lorem2 WHERE campo1 LIKE '%"&parole&"%' OR campo1 LIKE '%"&replace(parole," ","%")&"%'"
'(questo esempio è solo per campo1)
'sql = "SELECT * FROM lorem2 WHERE (("
ArrSearch = Split(parole)
x = 0
for each word In ArrSearch
word = trim(word)
If Len(checked1) <> 0 Then
sql = sql & " campo1 like '%" & word & "%'"
End If
If Len(checked2) <> 0 Then
If Len(checked1) <> 0 Then sql = sql & " or"
sql = sql & " campo2 like '%" & word & "%'"
End If
If Len(checked3) <> 0 Then
If Len(checked1) <> 0 Or Len(checked2) <> 0 Then sql = sql & " or"
sql = sql & " campo3 like '%" & word & "%'"
End If
If Len(checked4) <> 0 Then
If Len(checked1) <> 0 Or Len(checked2) <> 0 Or Len(checked3) <> 0 Then sql = sql & " or"
sql = sql & " campo4 like '%" & word & "%'"
End If
If Len(checked5) <> 0 Then
If Len(checked1) <> 0 Or Len(checked2) <> 0 Or Len(checked3) <> 0 Or Len(checked4) <> 0 Then sql = sql & " or"
sql = sql & " campo5 like '%" & word & "%'"
End If
sql = sql &")"
If Not x = UBound(ArrSearch) Then sql = sql & " or ("
x = x + 1
Next
sql = sql &")"
'apro il rs
rs.Open sql, objConn, 3, 3
if rs.eof then ' nessun record soddisfa la richiesta!
%>
<center><table border="0" width="80%">
<tr>
<td></td>
<td align="center">Nessuna inserzione soddisfa i criteri di ricerca</td>
<td>Nuova ricerca</td>
</tr>
</table>
</center>
<%
else
' XXXXXXXX CONTEGGIO PAROLE CHIAVE xxxxxxxxxxxxxxxxxxxxxxxxxx
do while not (rs.EOF)
id=rs.fields.item("id").value
if len(checked1) > 0 then
conto1 = lcase(rs.fields.item("campo1").value)
'lcase perchè l'istruzione "instr" usata nel conteggio è case-sensitive!
end if
if len(checked2) > 0 then
conto2 = lcase(rs.fields.item("campo2").value)
end if
if len(checked3) > 0 then
conto3 = lcase(rs.fields.item("campo3").value)
end if
if len(checked4) > 0 then
conto4 = lcase(rs.fields.item("campo4").value)
end if
if len(checked5) > 0 then
conto5 = lcase(rs.fields.item("campo5").value)
end if
x = 0
conteggio=0
ArrSearch = Split(parole)
for each word In ArrSearch
word = trim(word)
prova=conto1&conto2&conto3&conto4&conto5
tet=0
for y=1 to len(prova)
tet=instr(prova, word) 'okkio: instr è case-sensitive!
if tet > 0 then
conteggio=conteggio+1
prova=mid(prova,tet+1)
end if
next
x=x+1
next
' XXXXXXXXXXXXXX AGGIORNAMENTO CAMPO "NUMERO PAROLE CHIAVE" XXXXXXXXXXXXXXXXX
SQLcont= "UPDATE lorem2 SET campo6="&conteggio&" where id ="&id&""
objConn.Execute SQLcont
rs.movenext
loop
'chiudo rs
rs.close
set rs=nothing
'ZZZZZZZ PAGINAZIONE ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
numero=4 'records per pagina
controllo=numero 'contatore dei records per pagina
'dim rs2, sql2
Set rs2 = Server.CreateObject("ADODB.Recordset")
pag = TRIM(Request.QueryString("pag")) 'paginazione
If pag="" Then
pag = 1
Else
pag = CInt(pag)
End If
' XXXXXXXXXXX NUOVA ESTRAZIONE RECORDS .............. XXXXXXXXXXXXXXXXXXXXXX
sql2 = "SELECT * FROM lorem2 WHERE (("
ArrSearch = Split(parole)
x = 0
for each word In ArrSearch
word = trim(word)
If Len(checked1) <> 0 Then
sql2 = sql2 & " campo1 like '%" & word & "%'"
End If
If Len(checked2) <> 0 Then
If Len(checked1) <> 0 Then sql2 = sql2 & " or"
sql2 = sql2 & " campo2 like '%" & word & "%'"
End If
If Len(checked3) <> 0 Then
If Len(checked1) <> 0 Or Len(checked2) <> 0 Then sql2 = sql2 & " or"
sql2 = sql2 & " campo3 like '%" & word & "%'"
End If
If Len(checked4) <> 0 Then
If Len(checked1) <> 0 Or Len(checked2) <> 0 Or Len(checked3) <> 0 Then sql2 = sql2 & " or"
sql2 = sql2 & " campo4 like '%" & word & "%'"
End If
If Len(checked5) <> 0 Then
If Len(checked1) <> 0 Or Len(checked2) <> 0 Or Len(checked3) <> 0 Or Len(checked4) <> 0 Then sql2 = sql2 & " or"
sql2 = sql2 & " campo5 like '%" & word & "%'"
End If
sql2 = sql2 &")"
If Not x = UBound(ArrSearch) Then sql2 = sql2 & " or ("
x = x + 1
Next
sql2 = sql2 &")"
' XXXXXX .......... QUESTA VOLTA IN ORDINE DI NUMERO DI RICORRENZE DELLE PAROLE CHIAVE! XXXXXXXXXX
sql2=sql2 & " ORDER BY campo6 DESC"
'apro il rs
rs2.open sql2, objConn, 3, 3
tot = rs2.RecordCount 'conta i records nel recordset per la paginazione
%>
<table border="0" width="100%">
<tr>
<td></td>
<td align="center">La ricerca ha estratto <%=tot%> inserzioni rispondenti ai criteri inseriti</td>
<td>Nuova ricerca</td>
</tr>
</table>
<table width='100%' border='1' cellpadding="2" cellspacing="0">
<%
rs2.move numero*(pag-1)
do while not (rs2.EOF or controllo=0)
id=rs2.fields.item("id").value
campo1=rs2.fields.item("campo1").value
campo2=rs2.fields.item("campo2").value
campo3=rs2.fields.item("campo3").value
campo4=rs2.fields.item("campo4").value
campo5=rs2.fields.item("campo5").value
'xxxxxxxxx EVIDENZIAZIONE PAROLE CHIAVE xxxxxxxxxxxxxxxxx
x=0
'ArrSearch = Split(parole)
for each word In ArrSearch
word = trim(word)
if len(checked1) > 0 then
campo1=lcase(campo1)
'anche replace è case-sensitive: senza lcase non riconosce la sottostringa "word" (tutta minuscola)!
campo1=Replace(campo1, word, ""&word&"")
end if
if len(checked2) > 0 then
campo2=lcase(campo2)
campo2=Replace(campo2, word, ""&word&"")
end if
if len(checked3) > 0 then
campo3=lcase(campo3)
campo3=Replace(campo3, word, ""&word&"")
end if
if len(checked4) > 0 then
campo4=lcase(campo4)
campo4=Replace(campo4, word, ""&word&"")
end if
if len(checked5) > 0 then
campo5=lcase(campo5)
campo5=Replace(campo5, word, ""&word&"")
end if
x=x+1
next
'XXXXXXXXXXXXXX STAMPA A VIDEO XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
%>
<tr>
<td><%=campo1%></td>
<td><%=campo2%></td>
<td><%=campo3%></td>
<td><%=campo4%></td>
<td><%=campo5%></td>
<% controllo = controllo - 1
rs2.Movenext
loop
%>
</tr></table>
<center><table border="0" width="250">
<tr>
<%
If ( pag > 1) Then
%>
<td width="50" align="left">[img]barrow.gif[/img]</td>
<% else %>
<td width="50"></td>
<%
end if
%>
<td width="220" align="center">
<%
if tot mod(numero)=0 then
lastpage=int(tot/numero)
else 'deve numerare l'ultima pagina anche se incompleta!
lastpage=int(tot/numero)+1
end if
for c=1 to lastpage
%>
<%=c%>
<%
next
%></td><%
If not rs2.eof Then
%>
</td><td width="50" align="right">[img]arrow.gif[/img]</td>
<% else %> <td width="50"></td>
<% end if %>
</tr>
</table>
</center>
<div id="piedepagina">Pag. <%=pag%> di <%=lastpage%></div>
<%
rs2.close
set rs2=nothing
end if
end if
'chiudo tutto
call close_connection()
%>
</body>
</html>