Jump to content

controlling search variable


turpentyne

Recommended Posts

This may sound like an odd thought, but us beginners come up with weird ideas sometimes.

 

I'm trying to solve a "three letters or less" search. Basically if the user enters 4 words or more, my script does a fulltext search. If they do 3 letters or less, it does a basic search. But how can I write the script so that it only searches for those matching letters that have either a space in front or at the end... or that start with the three letters searched.

 

so, if they type in "elm"

 

I want to get "chinese elm"

I want to get "Elmendorf's onion"

I don't want to get "engelman's spruce"...

 

or at the very least have results with no space on either side be at the end of the results?

 

right now, I'm just doing a  basic search and a basic search that concatenates two fields:

$data = mysql_query("SELECT * FROM plantae WHERE common_name_english LIKE '%$item%'");

 

$data = mysql_query("SELECT * FROM plantae WHERE CONCAT(taxonomic_genus,' ',scientific_name) LIKE '%$item%'");

Link to comment
https://forums.phpfreaks.com/topic/251487-controlling-search-variable/
Share on other sites

Using fulltext search is a better option.

http://dev.mysql.com/doc/refman/5.6/en/fulltext-search.html

 

I could see doing something like this.

$data = mysql_query("SELECT * FROM plantae WHERE common_name_english LIKE '$item' AND common_name_english LIKE '% $item' AND common_name_english LIKE '$item %'");

 

I did this simple function to only display results as you stated.

 

<?php
$search_term = "elm";
$results = array("elm","chinese elm","Elmendorf's onion","engelman's spruce","Elm tree");

function excludeWithin($search_term,$words){
$search_term =strtolower(trim($search_term));
$words =strtolower(trim($words));

if($search_term == $words || preg_match("/ $search_term/",$words) || preg_match("/$search_term /",$words)){
return $words;
}
}

foreach($results as $result){
if(excludeWithin($search_term,$result) != ""){
echo $result."<br />";
}
}
?>

 

Results would be:

elm

chinese elm

Elm tree

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.