views:

84

answers:

3

in my search form, if the user types 'good', it displays all the results which contain the keyword 'good'. however if the user types in 'good sweetest', it displays no results because there is no record with the two words appearing together; BUT appearing in an entry at different places.

for example, the record says:

A good action is an ever-remaining store and a pure yield

the user types in 'good', it will show up this record, but if the user types in 'good' + 'pure', it will not show anything. or if the record contains the keyword 'good-deeds' and if the user types in 'good deeds' without the hyphen, it will not show anything.

what i would like is that if the user types in 'good' + 'pure' or 'good deeds' it should records containing these keywords highlighting them.

search.php code:

$search_result = "";

$search_result = $_POST["q"];

$search_result = trim($search_result);

//Check if the string is empty
if ($search_result == "") {
  echo  "<p class='error'>Search Error. Please Enter Your Search Query.</p>" ;
  exit();
      }

if ($search_result == "%" || $search_result == "_" || $search_result == "+" ) {
  echo  "<p class='error1'>Search Error. Please Enter a Valid Search Query.</p>" ;
  exit();
      }

$result = mysql_query('SELECT cQuotes, vAuthor, cArabic, vReference FROM thquotes WHERE cQuotes LIKE "%' . mysql_real_escape_string($search_result) .'%" ORDER BY idQuotes DESC', $conn)
  or die ('Error: '.mysql_error());


function h($s) {
    echo htmlspecialchars($s, ENT_QUOTES);
} 

function highlightWords($string, $word)
 {

    $string = preg_replace("/".preg_quote($word, "/")."/i", "<span class='highlight'>$0</span>", $string);
    /*** return the highlighted string ***/
    return $string;

 }

?>

<div class="caption">Search Results</div>
<div class="center_div">
<table>
    <?php while ($row= mysql_fetch_array($result, MYSQL_ASSOC)) {
        $cQuote =  highlightWords(htmlspecialchars($row['cQuotes']), $search_result);
        ?>
        <tr>
        <td style="text-align:right; font-size:18px;"><?php h($row['cArabic']); ?></td>
            <td style="font-size:16px;"><?php echo $cQuote; ?></td>
            <td style="font-size:12px;"><?php h($row['vAuthor']); ?></td>
            <td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']); ?></td>
        </tr>
    <?php } ?>
</table>
</div>

search.html:

   <form name="myform" class="wrapper">
      <input type="text" name="q" onkeyup="showUser()" class="txt_search"/>
      <input type="button" name="button" onclick="showUser()" class="button"/>
      <p>
        <div id="txtHint"></div>
    </form>
+2  A: 

What you want is called full-text searching. Put simply, full-text searching basically searches for each individual word on its own.

Michael Madsen
my table type is INNODB. .and unfortunately it says that it doesn't support FULLTEXT.
fuz3d
In that case, I suggest you go look at http://stackoverflow.com/questions/1381186/fulltext-search-with-innodb, which deals exactly with how to get around that problem. If a dedicated search engine, like suggested in that post, isn't possible for you, you can do other things, like use triggers to replicate your InnoDB data to a MyISAM table and search on that table, or rolling your own solution by manually splitting the content and linking words to the right post like Gabriel suggests.
Michael Madsen
thank you for your answers. have decided to go ahead with a php alternative. http://www.acuras.co.uk/articles/2-php-multi-word-mysql-search-algorithm-and-output
fuz3d
+1  A: 

You will want to implement content indexing to utilize this kind of search, essentially a relational table associating each word with a content component (like your record there), when you search query the index and return the associated content, also this allows you to change relevance by count, add fields to be indexed etc. http://www.databasejournal.com/sqletc/article.php/1578331/Using-Fulltext-Indexes-in-MySQL---Part-1.htm this might be a good start.

Gabriel
please see my above comment. my table type is INNODB. is there a workaround for this to make use of FULLTEXT in INNODB?
fuz3d
From what I can see it appears most users are creating a secondary ISAM table to handle this, I can only assume by reading the contents from the INNODB and creating the index records over in the ISAM where searches can be executed, and return not a result, but a query to the INNODB by key.
Gabriel
A: 

In my opinion, doing a search engine correctly for your site is one of the toughest things to do. I would just sign my site up on google and let google index it. You can customize the output of the page and integrate that look into your site.

The only evidence will be the "Google" branded search button, but to be honest, I think you can remove that now.

pinnacler