Location: PHPKode > projects > Phpman > phpman-2.0a_RC2/inc_search.php
<h1>Search</h1>

<?php
if(isset($submit) OR $_REQUEST["query_string"] != "") {
	$query_string = trim($_REQUEST["query_string"]);
	show_search_form($_REQUEST["query_string"]);
	
	$words = explode(" ",$_REQUEST["query_string"]);
	$anz_words = count($words);
	
	$string = "<h3>Searching for '";
	$i = 0;
	while($i < $anz_words) {
		$string .= "$words[$i]";
		if($i != $anz_words - 1) {
			$string .= ", ";
		}
		$i++;
	}
	$string .= "'</h3>";
	
	echo "$string\n";
	$ex1 = generateQuery($words,"t2.content",$_REQUEST["boolean"]);
	$ex2 = generateQuery($words,"t2.title",$_REQUEST["boolean"]);
	$query = "SELECT t1.*,t2.*,t3.*\n"
	."FROM `#__pages` AS t1\n"
	."INNER JOIN `#__pages_lang` AS t2 ON t1.page_ID=t2.page_ID\n"
	."INNER JOIN `#__languages` AS t3 ON t2.language_ID=t3.language_ID\n"
	."WHERE ".$ex1." OR ".$ex2;
	$database->setQuery($query);
	$rows = $database->loadRowsAssoc();
	
	/** get parents */
	$parents = array();
	for($i = 0; $i < count($rows); $i++) {
		$rows[$i]["content"] = red_term($rows[$i]["content"],$words);
		
		$query = parentQuery($rows[$i]);
		$database->setQuery($query);
		$parent_data = $database->loadRowsAssoc();
		if(count($parent_data) < 1) {
			$query = parentQuery($rows[$i],_mlsLang);
			$database->setQuery($query);
			$parent_data = $database->loadRowsAssoc();
		}
		
		$parents[$rows[$i]["page_ID"]] = $parent_data;
	}
	pmFrontendHTML::searchResults($rows,$parents);
	
} else {
	show_search_form();
}
?>


<?php
/**
* Generate the very nice query to fetch parents with language data
* @param array $row Page row data
* @param string $language A language isocode to fetch
* @return string $query
*/
function parentQuery($row,$language = '') {
	global $database;
	if(!$language) {
		$language = $row["isocode"];
	}
	$query = "SELECT `language_ID` FROM `#__languages` WHERE `isocode`='".$language."'";
	$database->setQuery($query);
	$temp = $database->loadRowAssoc();
	$language_ID = $temp["language_ID"];
	
	$query = "SELECT t1.*,\n"
		."t2.*,\n"
		."t3.*,\n"
		."t4.manual_ID as `manual_ID`,\n"
		."t4.pubdate as `manual_pubdate`,\n"
		."t5.title as `manual_title`,\n"
		."t5.subtitle AS `manual_subtitle`\n"
		."FROM `#__pages` AS t1\n"
		."INNER JOIN `#__pages_lang` AS t2 ON t1.page_ID=t2.page_ID\n"
		."INNER JOIN `#__languages` AS t3 ON t2.language_ID=t3.language_ID\n"
		."INNER JOIN `#__manuals` AS t4 ON t4.root_ID=t1.root_ID\n"
		."INNER JOIN `#__manuals_lang` AS t5 ON t4.manual_ID=t5.manual_ID\n"
		."WHERE t5.language_ID='".$language_ID."'\n"
		."AND t2.language_ID='".$language_ID."'\n"
		."AND t1.root_ID='".$row["root_ID"]."'\n"
		."AND t1.lft<'".$row["lft"]."'\n"
		."AND t1.rgt>'".$row["rgt"]."'\n"
		."ORDER BY  t1.lft ASC";
	
	return $query;
};

function generateQuery($words,$field,$boolean) {
	$i = 0;
	$exeptions = array();
	foreach($words as $word) {
		$exeptions[] = $field." LIKE '%".$word."%'";
		$i++;
	}
	$exeption = implode(" ".$boolean." ",$exeptions);
	return $exeption;
};

function show_search_form($query_string = "") {
	?>
	
	<table border="0" cellspacing="2" cellpadding="2">
	<form method="POST" action="?page=search">
	  <tr>
	    <td style="background-color: #bbbbbb" valign="top"><b>String:</b></td>
	    <td style="background-color: #eeeeee" valign="top"><input type="text" name="query_string" size="50" value="<?php echo $query_string; ?>"></td>
	  </tr>
	  <tr>
	    <td style="background-color: #bbbbbb" valign="top"><b>Boolean:</b></td>
	    <td style="background-color: #eeeeee" valign="top"><select name="boolean"><option value="AND" selected>AND</option><option value="OR">OR</option></select></td>
	  </tr>
	  <tr>
	    <td></td>
	    <td><input type="submit" name="submit" value="Search"></td>
	  </tr>
	</form>
	</table>
	
	<?php
};


function red_term($input_string,$words) {
	//Position des ersten Suchwortes (stripos)
	/** strip HTML */
	$input_string = ereg_replace("<([^>]+)>", "", $input_string);
	foreach($words as $word) {
		if(!$firstpos) {
			$firstpos = ($_t=stristr($input_string,$word)) ? strlen($input_string)-strlen($_t) : FALSE;
		}
	}
	$string = $input_string;
	if(strlen($string) <= 160) {
		//Wenn der String kleiner gleich 160 ist
		$string = $string;		
	} else {
		if($firstpos === FALSE) {
			//Wenn das erste Wort nicht vorkommt
			//nimm die ersten 160 Zeichen
			$string = substr($input_string,0,160);
		} else {
			if($firstpos < 80) {
				//Das erste wort ist innert der ersten 80 Zeichen
				//Starte am anfang
				$start = 0;
				$start_points = "";
			} else {
				//Das erste Wort ist nach dem 80 Zeichen
				//Starte 80 vor dem ersten Wort
				$start = $firstpos - 80;
				$start_points = "...";
			}
			
			//Ende 80 Zeichen hinter dem ersten Wort definieren
			$ende = $firstpos + strlen($words[0]) + 80;
			$end_points = "...";
			if($ende > strlen($input_string)) {
				$ende = strlen($input_string);
				$end_points = "";
			}
			$offset = $ende - $start;
			$string = substr($input_string,$start,$offset);
			$string = $start_points . $string . $end_points;
		}
	}
	
	//Rot hervorheben
	foreach($words as $word) { 
		$string = eregi_replace("($word)",'<span style="background-color: '.'yellow'.'">\\1</span>', $string);
	}
	return $string;
	
};

?>
Return current item: Phpman