[web] ASP: splitting keywords into array

Started by
5 comments, last by Michael Tanczos 18 years, 9 months ago
I have a search field which I am trying use to split the keywords into an array, based on spaces between keywords, then plug that into an SQL statement. For some reason when I run the code below, I get an 'Object required' error.

Dim i, sSearch, sArray
i=0
if sSearch <> "" then 
  sArray = split(sSearch," ")

   do while i<sArray.length
	  i = i+1
         sql = sql & " AND " 
      sql = sql & "Keywords LIKE '%" + sArray(i) + "'%"

  loop
   end if


Does it look like that code should work? Is there a good chance the error is being caused elswhere in the page? I am using an Access database for this.
Advertisement
Hrmm..that looks to me like it should work, although i'm no pro.

Does it tell you what line the error is on? It should if you're using regular ASP with VBScript
(ensure show friendly error messages is disabled) I.E. = Tools->Interent Options->Advanced->Show Friendly HTTP error messages

ArchG
sql = sql & "Keywords LIKE '%" + sArray(i) + "'%"


should be

sql = sql & "Keywords LIKE '%" + sArray(i) + "%'"


Notice position of apostrophe.
If this is old-school asp 3.0, then the problem is .length. There's no .length property on arrays. It's retarted, but you have to do something like this:

function ArrayLength(a)    dim length    on error resume next        length = 0        if isArray(a) then            length = ubound(a) - lbound(a) + 1        end if    on error goto 0    ArrayLength = lengthend function


If you try to do a ubound or lbound on something that is not an array or a zero-length array ( erase a ) then you will get an error. This is something that has annoyed me for years.
This is VBScript you're working with.

i = 0...do while i<=UBound(sArray)    sql = sql & " AND "     sql = sql & "Keywords LIKE '%" + sArray(i) + "'%"    i = i + 1loop


The array variable is not an object with properties and methods. Use UBound() to get the upper boundary of the array (and correct to <= since UBound returns the index of the last element, not the number of elements). Also move the increment to the end of the loop (you're skipping the first element, sArray(0)).
Awesome! Finally got it to work! Thanks for that peoples.
Try searching for this:
boolean's house

---
Michael Tanczos

This topic is closed to new replies.

Advertisement