Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

flaviora

Busca por qualquer palavra

Recommended Posts

Oi pessoal.

 

Estou fazendo uma busca em uma tabela de um access com esse script

 

<%

Dim cnConexao, rsBusca

 

Set cnConexao = Server.CreateObject("ADODB.Connection")

cnConexao.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("banco/jpturismo.mdb")&";"

 

Set rsBusca = Server.CreateObject("ADODB.RecordSet")

rsBusca.Open "SELECT * FROM busca WHERE nome LIKE '%"&Request.Form("buscando")&"%'",cnConexao,3,3

 

If rsBusca.EOF Then

Response.Write "Não foi encontrado nenhum registro!"

Else

While Not rsBusca.EOF

Response.Write rsBusca("nome") & "<br>"

rsBusca.MoveNext

Wend

End If

%>

 

Mas o que acontece é que vamos supor que exista o nome FRANSCISCO JOSE DA SILVA no banco.. e a pessoa pesquisar por FRANCISCO DA SILVA ele não acha nada porque tem o "JOSE" no meio.. e ele está procurando pelo nome exato!

 

Ou então se eu quiser procurar por 2 pessoas diferentes.. por exemplo digito na busca "RICARDO JOAO"

 

se no banco tem RICARDO DA SILVA e em outra linha "JOAO DA SILVA" nao acha nada..

 

agradeço muito a quem me ajudar.. preciso muito que isso dê certo!

Compartilhar este post


Link para o post
Compartilhar em outros sites

olha nesse código onde eu uso um form para usuário escolher na busca os critérios

AND(e), OR (ou), ou palavra EXATA

 

<form method="get" action="<%= strURL %>">
					<p>
					<input type="radio" name="m" value="1" <% IF strMethod = 1 THEN response.write "checked=""checked""" END IF%> />
						E
						<input type="radio" name="m" value="2" <% IF strMethod = 2 OR strMethod = "" THEN response.write "checked=""checked""" END IF%> />
						OU
						<input type="radio" name="m" value="3" <% IF strMethod = 3 THEN response.write "checked=""checked""" END IF%> />
				   	Exato</p>

					<p><input type="text" class="input3" name="q" value="<%= Trim(strSearch) %>" /></p>

					<p><input type="submit" value="Procura" />
					<br /><br /></p>

				  </form>
				  <%



If strSearch <> "" Then


		Session("strSearch") = strSearch

		IF isnumeric(Request.QueryString("t")) = True OR Request.QueryString("t") = "" THEN

						  ' Retrieve t

						  If Request.QueryString("t") = ""  OR Request.QueryString("t") < 1 Then
							searchTable = 1 '1
						  Else

							searchTable = 1 'CInt(Request.QueryString("t"))

						  End If

		ELSE

		searchTable = 1' 1

		END IF




		' THE TABLES

		Select Case searchTable

			Case CInt("1")

					dbTable = "tMessages"
					'strDBPath = Server.MapPath("/blogg/blog.mdb")


			Case CInt("2")

					'dbTable = "events"
					'strDBPath = Server.MapPath("")

			Case Else

					dbTable = "tMessages"
					'strDBPath = Server.MapPath("")


		End Select



'------------------ SQL



				  'SPLIT STRING INTO ARRAYS
				  'FOR and AND or CLAUSE SQL


'----------------- SQL tMessages blog table

IF searchTable = "1" THEN
IF strMethod < 3 THEN


			If strMethod = 1 Then
			strSplit = Replace(strSearch," "," and ")
			Else
			strSplit = Replace(strSearch," "," or ")
			End If


			colTerms = split(strSplit," and ",-1,1)

			iUpperTerms = UBound(colTerms)

			strWhereClause = ""



			'FIELD CLAUSES

			nr = 0
			Do while nr <= 5

			nr = nr + 1

			searchField = CInt(nr)

			Select Case searchField

				Case CInt("1")

					strTableInClause = "fDate"

				Case CInt("2")

					strTableInClause = "fHeadline"

				Case CInt("3")

					strTableInClause = "fMessage"

				Case CInt("4")

					strTableInClause = "fAuthor"

				Case CInt("5")

					strTableInClause = "fnota"
				
				'Case Else


			End Select


				  For iLoop = LBound(colTerms) to iUpperTerms
					orTerms = split(colTerms(iLoop)," or ",-1,1)
					iUpperOrTerms = UBound(orTerms)
					strWhereClause = strWhereClause & ""

						for iOrLoop = LBound(orTerms) to iUpperOrTerms
								strWhereClause = strWhereClause & strTableInClause & " LIKE '%" & Trim(orTerms(iOrLoop)) & "%' "

							if iOrLoop < iUpperOrTerms then
								strWhereClause = strWhereClause & " OR "
							end if
						next

						strWhereClause = strWhereClause & ""

						if iLoop < iUpperTerms then
								strWhereClause = strWhereClause & " AND "
						end If

				  Next

				  if nr <= 4 then
				  strWhereClause = strWhereClause & " OR "
				  end if

		Loop


ELSE

' METHOD 3 EXACT PHASE WHERE

strWhereClause = "fDate LIKE '%" & strSearch & "%' OR fHeadline LIKE '%" & strSearch & "%' OR fMessage LIKE '%" & strSearch & "%' OR fAuthor LIKE '%" & strSearch & "%'  OR fnota LIKE '%" & strSearch & "%'"

END IF


strWhereClause2 = "WHERE (" & strWhereClause & ") AND (online = CBool(-1))"

' ORDER BY
strOrderClause = "ORDER BY fdate DESC"



	IF strMethod = 3 THEN

		strSQL = "SELECT * FROM "& dbTable &" "& strWhereClause2 &" "& strOrderClause &";"

	ELSE

		strSQL = "SELECT * FROM "& dbTable &" "& strWhereClause2 &" "& strOrderClause &";"

	END IF


END IF

Response.write strSQL

Compartilhar este post


Link para o post
Compartilhar em outros sites

Eh pq você ta usando o porcento % entre a string a ser buscado no banco.

se você por entre, soh na esquerda ou soh na direita ele busca todos usando a condição OR e AND.

 

like '%string%' or

like 'string%' or

like '%string'

Compartilhar este post


Link para o post
Compartilhar em outros sites

Usa o meu exemplo, oferece mais critério para o user...

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.