XML-Based Searchable Database (part 4): text search

Download files

The SWF

Type in for example Monica Schneider or weight or other.



The Actionscript

stop();

//loading an introductory swf
loadMovie("agency.swf","modelHolder");

//asfunction to load pics from the xml file.
function loadPics(movie){
	modelHolder.loadMovie(movie);
}

//asfunction to go to a different frame from xml files
function goFrame(frame){
	gotoAndPlay(frame);
}
//scrollbar target is defined here
scrollBar.target = InstanceName_0;

//this is the function for the search engine
function searchText() {
	model = new XML();//defining a new instance
	model.ignoreWhite = true;
	model.load("fmodels.xml");//loading the xml
	//function to control the loading
	model.onLoad = function (success){
		if (success){
			headline.htmlText="Welcome to Eye Catch Models";
			newModel();
		}else{
			headline.htmlText="loading...";
		}
	}
	//function to parse the xml
	function newModel() {	
		for (var count01=0; count01<= model.childNodes.length; count01++) {
			if (model.childNodes[count01].nodeName.toLowerCase() == "models") {
				modelsDescryption = model.childNodes[count01];
			}
		}findModels(modelsDescryption);	
	}
	//function to define all childnodes and all the variables
	function findModels(myModel) {
		allModels = [];//create empty array here for the list
		//we loop through all the childnodes. Since the last node is undefined
		//we set the length subtracting 1, the last node.
		for (var count02=0; count02<=myModel.childNodes.length-1; count02++) {
			//defining the variables modelName, pic, webSite and modelsDescryption
			modelName = myModel.childNodes[count02].attributes.name;
			pic = myModel.childNodes[count02].attributes.photo;
			webSite = myModel.childNodes[count02].attributes.id;
			modelsDescryption = myModel.childNodes[count02].firstChild;
			//fill the array with content and add links and html format
			allModels.push(modelName+"\n"+"(Photo)"+"  "+"(Web site)\r"+modelsDescryption+"\r");
			headline.htmlText = "";
			//to show the array content in the textfield
			InstanceName_0.htmlText = allModels;
			//the actual text search function
			searchTheText();
		}
	}
}
//the search function
function searchTheText(){
	//first define what happens when there is no search word entered
	if (searchWord.text == ""){
		//we use the headline textbox for the output
		headline.htmlText = "Enter a search word!";
		//we don't want anything shown in the main textfield wiyhout any search
		InstanceName_0.htmlText = "";
	}//in case there is a search word, set a variable (startpos) to the index
	//position in the textfield, which is the xml childnode with text
	startPos = InstanceName_0.text.indexOf(searchWord.text);
	if(startPos != -1){
		//calculate the endposition of the search by using the length
		//of the search word
   		endPos = startPos + searchWord.length;
		//set focus on the textfield
    	Selection.setFocus(InstanceName_0);
		//select the found search word and highlight
    	Selection.setSelection(startPos,endPos);
  	} else {
		//if the search word was not found, print a message
    	headline.htmlText = "Sorry, no match found.";
		//do not show the text to be searched in the textfield
		InstanceName_0.htmlText = "";
	}				
}