Simple Search Engine PHP/MySQL

Having a search engine on your site can be a great thing. Many times users prefer searching rather than browsing in order to find something on your site. Google does offer site search that will index your site, but I like many other people prefer to keep things in-house so to speak. Therefore, I created a simple search engine that you are free to modify to meet your needs. Currently, there is very little logic incorporated into the search but will due for most sites.

This is a callback function to filter the length of the search criteria to only those terms larger than three characters.

function criteriaLength($criteria){

	if(strlen($criteria) >= 3){

		return true;

	}else{

		return false;

	}
}

Below is the search engine itself, that takes a string criteria and a comma separated list for the search fields.

 function search($criterion, $searchFields){

	//Trim the whitespace from the $criteria
	$criterion = trim($criterion);
	$criterion = explode(" ", $criterion);

	//Filter the criteria for specific length
	$criterion = array_filter($criterion, "criteriaLength");

	//Reset the array index after the filter
	$criterion = array_merge($criterion);

	//Check to see if a search criteria exists
	if(count($criterion) > 0){

	//Explode the criterion to get the search terms
	$numCriteria = count($criterion);

	//Generate SQL Query to search base on fields
	$query = "";

	//Check if there are search fields
	if($searchFields = explode(",",$searchFields)){

	//Explode the search fields string based on commas
	$numFields = count($searchFields);

	//Loop over each field to be aliased
	for($i = 0; $i < $numFields; $i++){

		//Alias the searchfields arrays to match the table structure
		switch($searchFields[$i]){
			case "firstname":
			 $searchFields[$i] = "";
			break;
			case "lastname":
				$searchFields[$i] = "";
				break;
			case "email":
				$searchFields[$i] = "";
				break;
			case "dob":
				$searchFields[$i] = "";
				break;
			case "username":
				$searchFields[$i] = "";
				break;
			case "all":
				//Manually set the columns to be searched on if none were defined
				$searchFields[0] = "";
				$searchFields[1] = "";
				$searchFields[2] = "";
				$searchFields[3] = "";
				$searchFields[4] = "";
				$numFields = count($searchFields);
				break;
                       default:
		}
	}

	}else{

	//Create the search fields array if since there were no search fields defined
	$searchFields = array();

	//Manually set the columns to be searched on
	$searchFields[0] = "";
	$searchFields[1] = "";
	$searchFields[2] = "";
	$searchFields[3] = "";
	$searchFields[4] = "";

	//Get the number of elements in the
	$numFields = count($searchFields);
	}

			//Loop over criteria to generate like statements for each search field
			for($a = 0; $a < $numCriteria; $a++){

				for($i = 0; $i < $numFields; $i++){

					$query = $query." $searchFields[$i] LIKE  '$criterion[$a]%' ";

					//Add OR statements to end of each like, except last one
					if($i != $numFields - 1){

						$query = $query." OR ";
					}
				}

				//Add OR statements to end of each like for the criteria, except last one
				if($a != $numCriteria - 1){
					$query = $query." OR ";
				}
			}

		$query = $query." ORDER BY (";

		//Loop over criteria again, this time to generate case statements in order to calculate relevance
		for($a = 0; $a < $numCriteria; $a++){

				for($i = 0; $i < $numFields; $i++){
                                  $query = $query." ( CASE WHEN $searchFields[$i] LIKE  '$criterion[$a]%' THEN 1 ELSE 0 END ) "; 					 					//Add + statements to end of each like, except last one
                                  if($i != $numFields - 1){
                                     $query = $query." + ";
                               }
}
//Add + statements to end of each like for the criteria, except last one
if($a != $numCriteria - 1){
$query = $query." + ";
}
}
$query = $query.") DESC";
print_r($query);
/* 	 		//Open Global mySQLi Connection
global $conn;
if ($conn->errno()){

			$returnData = $conn->error(); 

		}else{

			//Clean the query string
			$query = $conn->clean($query);

			//Execute the query and return result set
			if($result = $conn->query($query)){

				//Get an associative array from the result set
				$returnData = $result->fetch_array(MYSQLI_ASSOC); ;

			}else{

				$returnData = false;

			}
		}
	*/
	}else{

		$returnData = false;

	}

	return $returnData;

}