Multi word search in PHP/MySQL -
i'm struggling create search searches multiple words. first attempt yielded no results whatsoever , follows:
require_once('database_conn.php'); if($_post){ $explodedsearch = explode (" ", $_post['quicksearch']); foreach($explodedsearch $search){ $query = "select * jobseeker forename '%$search%' or surname '%$search%' order userid limit 5"; $result = mysql_query($query); } while($userdata=mysql_fetch_array($result)){ $forename=$userdata['forename']; $surname=$userdata['surname']; $profpic=$userdata['profilepicture']; $location=$userdata['location']; echo "<div class=\"result\"> <img class=\"quickimage\" src=\"" . $profpic. "\" width=\"45\" height=\"45\"/> <p class=\"quickname\">" . $forename . " " . $surname . "</p> <p class=\"quicklocation\"> " . $location . "</p> </div>"; } }
i tried following, yielded results, can imagine, getting duplicate results every word entered:
if($_post){ $explodedsearch = explode (" ", $_post['quicksearch']); foreach($explodedsearch $search){ $query = "select * jobseeker forename '%$search%' or surname '%$search%' order userid limit 5"; $result .= mysql_query($query); while($userdata=mysql_fetch_array($result)){ $forename=$userdata['forename']; $surname=$userdata['surname']; $profpic=$userdata['profilepicture']; $location=$userdata['location']; echo "<div class=\"result\"> <img class=\"quickimage\" src=\"" . $profpic. "\" width=\"45\" height=\"45\"/> <p class=\"quickname\">" . $forename . " " . $surname . "</p> <p class=\"quicklocation\"> " . $location . "</p> </div>"; } } }
i'm pretty @ loss how proceed this, appreciated.
edit:
if($_post){ $quicksearch = $_post['quicksearch']; $explodedsearch = explode (" ", trim($quicksearch)); $queryarray = array(); foreach($explodedsearch $search){ $term = mysql_real_escape_string($search); $queryarray[] = "forename '%" . $term . "%' surname '%" . $term . "%'"; } $implodedsearch = implode(' or ', $queryarray); $query="select * jobseeker ($implodedsearch) order userid limit 5"; $result = mysql_query($query); while($userdata=mysql_fetch_array($result, mysql_assoc)){ $forename=$userdata['forename']; $surname=$userdata['surname']; $profpic=$userdata['profilepicture']; $location=$userdata['location']; echo "<div class=\"result\"> <img class=\"quickimage\" src=\"" . $profpic. "\" width=\"45\" height=\"45\"/> <p class=\"quickname\">" . $forename . " " . $surname . "</p> <p class=\"quicklocation\"> " . $location . "</p> </div>"; } }
i've been working on same subject (search keywords) while , how did :
$words = $_post['keywords']; if(empty($words)){ //redirect somewhere else! } $parts = explode(" ",trim($words)); $clauses=array(); foreach ($parts $part){ //function_description in case , replace whatever u want in ur table $clauses[]="function_description '%" . mysql_real_escape_string($part) . "%'"; } $clause=implode(' or ' ,$clauses); //select condition , add "and ($clauses)" . $sql="select * functions user_name='{$user_name}' , ($clause) "; $results=mysql_query($sql,$connection); if(!$results){ redirect("errors/error_db.html"); } else if($results){ $rows = array(); <?php while($rows = mysql_fetch_array($results, mysql_assoc)) { // echo whatever u want ! } ?>
-- how when tried run fulltext search : should set table type "myisam"
<?php $words = mysql_real_escape_string($_post['function_keywords']); if(empty($words)){ redirect("welcome.php?error=search_empty"); } //if columns(results)>1/2(columns) => return nothing!(use "natural language"="boolean") $sql="select * functions match (function_description) against ('{$words}' in natural language mode)"; $results=mysql_query($sql,$connection); if(!$results){ redirect("errors/error_db.html"); } else if($results){ $rows = array(); while($rows = mysql_fetch_array($results, mysql_assoc)) { // echo } } ?>
Comments
Post a Comment