• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

PHP flexible search

Hi

I have a site that sells electronic products. If I have HP 4700 pocket pc and customer writes HP pocket pc to the search text field, it will not find it if I use "like" word of sql. What you can offer me that I can use for flexible search.

Thanks
0
hasozduru
Asked:
hasozduru
  • 3
  • 3
  • 2
4 Solutions
 
webpolsolCommented:
Here use this sql command

$query is equaled to your search box/text that the client is inputting

$query = $_REQUEST['query'];

$sql = mysql_query("SELECT * FROM `address` WHERE `email` LIKE '%$query%'");
               echo "<b>Results</b>";
               while ( $field = mysql_fetch_array( $sql) ) {
           echo "</br></br>{$field['ID']} - <u>{$field['email']}</u></b>";
           }
               mysql_free_result( $sql );
}

the %% are wild cards to pick up anything containing TEXT in that search so. . .  if they type "HP" it will show all products that contain the string "HP" including HP 4700 pocket pc.

same if they type "4700" it will still bring up all products containg "4700" including HP 4700 pocket pc.!

should solve your problem.

Ian
0
 
ldbkuttyCommented:
webpolsol,
no offense but LIKE operator is not what the author wants. Please read the question properly.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
webpolsolCommented:
He wants a flexible working search. . .

he mentioned that LIKE didnt work for him thats all.

Let him try it and see if it's what he wants himself before passing judgement.

0
 
German_RummCommented:
webpolsol,

ldbkutty is right, LIKE "%HP pocket pc%" will not find "HP 4700 pocket pc"
and FULLTEXT search will not work either without some mysql tweaking - 'pc' and 'hp' are less than 3 symbols in length, so they will be disregarded by default fulltext search.

little helpful function:

/**
 * $searchTerm - the words you want to search for
 * $field - Table field that should be tested
 * $condition - AND search all words, OR - search any of the words
 */
function createLikeClause($searchTerm, $field, $condition = 'OR') {
    $terms = explode(' ', $searchTerm); // first we split string for words
    function addLike(&$arr, $key, $field) {
        if ($arr == '')
            return;
        else $arr = $field.' LIKE "%'.$arr.'%"';
    }
    array_walk($terms, 'addlike', $field);
    return implode(' '.$condition.' ', $terms);
}

USAGE:
createLikeClause('HP Pocket PC', 'ProductName');
// returns `ProductName LIKE "%HP%" OR ProductName LIKE "%Pocket%" OR ProductName LIKE "%Pc%"`

createLikeClause('HP Pocket PC', 'Product', 'AND');
// returns `Product LIKE "%HP%" AND Product LIKE "%Pocket%" AND Product LIKE "%Pc%"`
0
 
German_RummCommented:
Real life usage:

$sql = 'SELECT * FROM tblProduct WHERE '.createLikeClause('HP Pocket PC', 'Title', 'AND').' AND InStock = 1';
// $sql: SELECT * FROM tblProduct WHERE Title LIKE "%HP%" AND Title LIKE "%Pocket%" AND Title LIKE "%Pc%" AND InStock = 1

Of course, there is plenty of room for modification :-)
0
 
ldbkuttyCommented:
Hello German_Rumm,
Thanks and Yes, you're absolutely right in the restriction of minimum length in FULLTEXT. word-by-word search is flexible, but i think its too much flexible. For example - for this search query: "HP 4700 pocket pc", the records are retrieved for all records that have any of the word in "hp", "4700", "pocket", "pc" !
0
 
German_RummCommented:
ldbkutty,

that's why I added 3rd parameter to function - $condition :-)
when $condition == 'AND' you will find only those records that have all the words together.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now