PDA

View Full Version : Search feature question.


kenji_dreamer
06-25-2008, 03:50 AM
Good day fellows, is it possible in flash that search field will display relevant data in search. Like in google. Every time you type word in the field it will give some sort of pull down menu that is related to your tag. I have attached file for clarify of what im trying to describe.. thanks a lot..

danjp
06-25-2008, 03:40 PM
hi kenji_dreamer

this is possible but you have to have some data to cross reference your string with. What is the context of this search box?

kenji_dreamer
06-26-2008, 03:14 AM
thanks danjp for the reply, i appreciate it. The context of the search box is keyword, tag or id of different subject(example anatomy). Can you tell how to do this? or what i need to know to accomplish it.

danjp
06-26-2008, 09:37 AM
well basically you need to have a list of words to compare against, then as the user types you take the string that they have typed and loop through the list, comparing all the words (and substrings within). The code to achieve this isn't complicated, however you'll need to display your results, and then use them appropriately.
That is the concept.

Comparing a string against an array might look something like this (for an exact match only)

var myWordsArray:Array = new Array(/* insert words here */ )
var mySearchString:String = /* the typed string */
for(var i:Number=0; i<myWordsArray.length; i++){
if(mySearchString == myWordsArray[i]){
//add the array word to the list
}
}


You might need to do things like add a Key Listener to update the search every time the press a key, and then update the list. Also searching for substrings within strings is a little more complicated, but not that tricky.
There are other things to consider like case-sensitivity, as AS2.0 is case sensitive, so if your user is searching for 'APPLE' or 'Apple' and your record of it is 'apple', then there won't be a match. One easy way to get around this is to set everything toLowerCase, then you can compare.


if(mySearchString.toLowerCase() == myWordsArray[i].toLowerCase()){

kenji_dreamer
06-27-2008, 02:19 AM
wow, thanks for the tip danjp. :) what would you recommend if the word which compare against come from server-side script. Is it in the form of XML or flashVars or do you any alternative idea?

danjp
06-27-2008, 11:21 AM
You could get that data in an XML file but this isn't something that you'd really be wanting to do every search.

I imagine that you'd load your XML first and then just use that to search through

kenji_dreamer
06-28-2008, 09:51 AM
you big help danjp.. I will start working on it.. Thanks a lot for the idea. :)