Sign in to follow this  
boolean

[web] ASP: splitting keywords into array

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 = length
end 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.

Share this post


Link to post
Share on other sites
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 + 1
loop


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)).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this