Avatar billede kkd Nybegynder
01. maj 2003 - 12:04 Der er 9 kommentarer og
1 løsning

Et or for meget...

Hej, jeg har en søgefunktion som benytter checkbokse til at opstillekriterier med, som den henter data fra en access database med.
Problemet er, at hvis man markerer mere end 2 checkbokse, så kommer der et or for meget - markerer man 3, kommer der to or, marker man fire kommer der tre o.s.v.

Her er et uddrag fra scriptet:
if request("str1") <> "" then
strSQL = strSQL & "str='" & request("str1") & "' "
end if
if request("str1") <> "" and request("str2") <> "" then strSQL = strSQL & "or " end if
if request("str1") <> "" and request("str3") <> "" then strSQL = strSQL & "or " end if
if request("str1") <> "" and request("str4") <> "" then strSQL = strSQL & "or " end if
if request("str1") <> "" and request("str5") <> "" then strSQL = strSQL & "or " end if
if request("str2") <> "" then
strSQL = strSQL & "str='" & request("str2") & "' "
end if
if request("str2") <> "" and request("str3") <> "" then strSQL = strSQL & "or " end if
if request("str2") <> "" and request("str4") <> "" then strSQL = strSQL & "or " end if
if request("str2") <> "" and request("str5") <> "" then strSQL = strSQL & "or " end if
if request("str3") <> "" then
strSQL = strSQL & "str='" & request("str3") & "' "
end if
if request("str3") <> "" and request("str4") <> "" then strSQL = strSQL & "or " end if
if request("str3") <> "" and request("str5") <> "" then strSQL = strSQL & "or " end if
if request("str4") <> "" then
strSQL = strSQL & "str='" & request("str4") & "' "
end if
if request("str4") <> "" and request("str5") <> "" then strSQL = strSQL & "or " end if
if request("str5") <> "" then
strSQL = strSQL & "str='" & request("str5") & "' "
end if

Jeg har sat en
response.write strsql
response.end
ind før sætningen eksekveres, og her er meddelelsen, hvis man har markeret de fire første (str1,2,3 og 4):

Select * from Cichlider where (str='str1' or or or str='str2' or or str='str3' or str='str4' )
Avatar billede roenving Novice
01. maj 2003 - 12:22 #1
Hej

Lidt programmeringsteknisk kan vist gøre det mere overskueligt:

s(1) = request("str1")
s(2) = request("str2")
s(3) = request("str3")
s(4) = request("str4")

for i = 1 to 4
  if s(i) <> "" then
    if strSQL <> "" then
      strSQL = strSQL & "or " & s(i)
    else
      strSQL = s(i) & " "
    end if
  end if
next

Nemmere, ikke?

hth
jes
Avatar billede Slettet bruger
01. maj 2003 - 12:25 #2
Kunne du ikke lave noget med
strSQL= ""
if chkbox1 then
  strSQL = strSQL & " felt = vaerdi OR "
end if
if chkbox2 then
  strSQL = strSQL & " felt = vaerdi OR "
end if
.
.
.
if chkbox99 then  (eller hvor mange du nu har)
  strSQL = strSQL & " felt = vaerdi OR "
end if

strSQL = "SELECT * from cichilider where " & mid(strSQL, 1, len(strSQL) - 3) 'Fjerner den sidste OR
Avatar billede roenving Novice
01. maj 2003 - 12:25 #3
Hov,

jeg glemte et

& " "
i en linje:

      strSQL = strSQL & "or " & s(i) & " "

jes
Avatar billede kkd Nybegynder
01. maj 2003 - 13:24 #4
Jes>>> Jeg kan ikke hitte ud af at sætte det ind i scriptet... Er det for meget forlangt, hvis jeg beder dig om at skrive det ind i det uddrag jeg har vist...
Avatar billede roenving Novice
01. maj 2003 - 13:50 #5
Du sletter:

if request("str1") <> "" then
strSQL = strSQL & "str='" & request("str1") & "' "
end if
if request("str1") <> "" and request("str2") <> "" then strSQL = strSQL & "or " end if
if request("str1") <> "" and request("str3") <> "" then strSQL = strSQL & "or " end if
if request("str1") <> "" and request("str4") <> "" then strSQL = strSQL & "or " end if
if request("str1") <> "" and request("str5") <> "" then strSQL = strSQL & "or " end if
if request("str2") <> "" then
strSQL = strSQL & "str='" & request("str2") & "' "
end if
if request("str2") <> "" and request("str3") <> "" then strSQL = strSQL & "or " end if
if request("str2") <> "" and request("str4") <> "" then strSQL = strSQL & "or " end if
if request("str2") <> "" and request("str5") <> "" then strSQL = strSQL & "or " end if
if request("str3") <> "" then
strSQL = strSQL & "str='" & request("str3") & "' "
end if
if request("str3") <> "" and request("str4") <> "" then strSQL = strSQL & "or " end if
if request("str3") <> "" and request("str5") <> "" then strSQL = strSQL & "or " end if
if request("str4") <> "" then
strSQL = strSQL & "str='" & request("str4") & "' "
end if
if request("str4") <> "" and request("str5") <> "" then strSQL = strSQL & "or " end if
if request("str5") <> "" then
strSQL = strSQL & "str='" & request("str5") & "' "
end if

og indsætter:

s(1) = request("str1")
s(2) = request("str2")
s(3) = request("str3")
s(4) = request("str4")

for i = 1 to 4
  if s(i) <> "" then
    if strSQL <> "" then
      strSQL = strSQL & "or " & s(i) & " "
    else
      strSQL = s(i) & " "
    end if
  end if
next


Så nemt er det!

hth
jes
Avatar billede roenving Novice
01. maj 2003 - 13:54 #6
Undskyld linjen skal være:

strSQL = strSQL & "or str='" & s(i) & "' "

og den anden

strSQL = "str='" & s(i) & "' "

Hvis du skal hav forskellige identifiers ('str=') kan du opbygge dem som i starten som f.eks. f(1), f(2) osv.

hth
jes
Avatar billede kkd Nybegynder
01. maj 2003 - 14:03 #7
Jeg for en
Type mismatch: 's' fejlmeddelelse!!!
Avatar billede roenving Novice
01. maj 2003 - 14:10 #8
Måske skal du erklære variablen:

dim s(4)
s(1) = ...
...

før du skriver i den.

Er usikker om den slags er nødvendigt i ASP-scripts.
Avatar billede kkd Nybegynder
01. maj 2003 - 14:40 #9
Det hjalp....

Der er stadig problemer, men det skyldes sansynligvis at jeg ikke har ændret i resten af scriptet!

Thank you!
Avatar billede roenving Novice
01. maj 2003 - 14:51 #10
Takker for points
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester