PDA

View Full Version : Search feature for ASP page...


reigalz
04-06-2003, 03:51 PM
hi all, i'm doing a e-commerce web site for my asp project...
i have a search feature on my web page where the users can enter the products they want to search for...
when i display the search results, how do i bold all the words that the user had search for?
for example, if the user search for 'walkman', when i display the search results, all the words 'walkman' will be bold.

Mhtml
04-06-2003, 04:33 PM
Umm.. well I'm really tired so I can't be held responsible for posting a wrong answer! There are 2 ways you can do it.

1) Split up the name/descript or whatever and go through the array bolding the words and then join it all.

2) Regular expressions, I'm to tired to do that so I'll leave that for Whammy, he's really good at it! :)


dim description, searchWord
searchWord = request.Form("searchQuery")
description = rs("ProductDescrpition")
description = split(" ", description)
for i = 0 to ubound(description)
if description(i) = searchWord then
description(i) = "<b>"&description(i)&"</b>
next
description = join(" ", description)
response.write(description)


:) I hope that's right, my fingers are doing the thinking coz brain status = off.

reigalz
04-06-2003, 06:29 PM
i dun really understand wat the code means....can u pls explain if possible? or is there any other easier way? thanks alot!

Mhtml
04-07-2003, 08:00 AM
Well the RegExp code will be a lot smaller but more complex, well at least in my feeble mind.

Say we have a string we've pulled from the database containing an item's description for example:

myString = rs("Description")


Using split() we can separate each individual word!

arrString = split(" ", myString)

That code will set arrString as an array containing each word from the original string value.


for i = 0 to ubound(arrString)
if arrString(i) = searchString then
arrString(i) = "<b>"&arrString(i)&"</b>"
next

Now the above will do a loop through each number in a specified range, in this case the range is 0 to ubound(arrString).

What ubound(arrString) does is count how many entries there are in the array arrString.

After it gets the range it moves to the item arrString(i) which if i is = 2 at the time would be arrString(2) which would pull the 3rd record out of the array because it is zero based.

Now if arrString(i) is equal to the searchValue variable which could very well be walkman it will turn that entry into bold.

Once that is all done, we do the opposite of split() and join them using a space between each word because that's how we split them up.

myString = join(" ", arrString)


If you do that for every value you are pulling from the database then it will bold the search term for each entry.

I hope I explained that well enough, note that this is a slightly simplyfied version of the code I made for you.

reigalz
04-07-2003, 11:58 AM
thanks for the explanation :) i understand more after reading it...
but for example, there's a product model which is D-E2000 then the user search for 2000...then how do i bold the 2000 only? i tink it could not be done by using the method u mentioned coz there are not spaces in between...

Mhtml
04-07-2003, 12:10 PM
That was my main concern, also if for instance you forgot to put a space between onewordandthenext like that it wouldn't show up. :)..

I'm a little more awake at the moment so I will have ago at the other method. :) Be right back!