HomeAbout MeResumeKnowledgeProgrammingSource CodeSearch Code

Search Code

Code Demo

Below is what the script looks like that displays the search results for the ryanfyfe.com site search:

# STARTS RESULTS

if($total_results>0){
	
	# PUT SEARCH WORDS INTO AN ARRAY
	if(strpos($_GET['q'], " ")){
		$search_words = explode(" ", $_GET['q']);
	} else {
		$search_words[] = $_GET['q'];
	}			

	# LOOP THROUGH RESULTS		
	while($result = mysql_fetch_assoc($query)){
			
		# EMPTY TEMP VARIABLES
		unset($tmp, $first_location, $snippet, $replace, $start, $find);
			
		# START SNIPPET CREATION
		$snippet = strtolower(strip_tags($result['content']));
			
		# FIND POSITION OF FIRST KEYWORD
		foreach($search_words as $find){
			$tmp = strpos($snippet, $find);
			if(empty($first_location)) $first_location = $tmp;
			if($tmp < $first_location){
				$first_location = $tmp;
			}
		}
		
		# CUT SNIPPET FOR RELEVANT PREVIEW
		$start = (($first_location-150) < 0) ? "0" : ($first_location-150);
		$snippet = strtolower($result[description])." ".substr($snippet, $start, 300);	

		# HIGHLIGHT ALL KEYWORDS IN SNIPPET
		foreach($search_words as $find){
			$replace = '< b>'.$find.'< /b>';
			$snippet = str_replace($find, $replace, $snippet);
		}
		$snippet = ucfirst($snippet);

		# MAKE PATH TO PAGE
			unset($path);
			$id = $result[id];
			$cont = true;

			while($cont == true){
				$title = pagetitle($id);
				$title = $title['title'];
				$path = "< a href='"._sbase_.mkurl($title)."_$id.html'>".$title."< /a> ⇒ ".$path;
				if($parent = get_parent($id)){
					$id = $parent['page_id'];
				} else {
					$cont = false;
				}
			}


		# DISPLAY ROW HERE - LINK/PATH/SNIPPET

	}
}

# END RESULTS