Jump to content

Recommended Posts

Hi, I'm trying to improve my search so that it searches all words that i write in the search box and matches keywords

 

my code right now is


<?php
$sub = $_GET['article'];

echo "<h2>search results for: $sub</h2><br><br>";


$sub = str_replace(" ", "%", $sub);

$alb = 	mysql_connect("localhost", "", "") or die(mysql_error());  
mysql_select_db("articles") or die(mysql_error()); 

mysql_set_charset('utf8', $alb); 

$query2 = "SELECT * FROM `blog` WHERE `title` like '%$sub%'";
$result2 = mysql_query($query2) or die(mysql_error());


while ($postede2 = mysql_fetch_assoc($result2))
{
$title = "{$postede2['title']}";
$author = "{$postede2['author']}";
$section = "{$postede2['section']}";
$description = "{$postede2['description']}";
$url = "{$postede2['url']}";

echo "<a href='$url'><h3><b>$title</b></h3></a><br>";
echo "By $author, Category: $section<br>";
echo "$description ...<br><br>";

}
?>

 

How do i break down the search even further?

Link to comment
https://forums.phpfreaks.com/topic/251084-improving-my-search/
Share on other sites

If want a more advanced search I suggest using mysql fulltext search.

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

Be sure to read all about it.

 

To use more words as search terms, you would explode the POST or GET array. In your case that would be $sub.

You should also sanitize/escape any user input before it gets to your query.

 

If would like to search in specific areas, use a form to select that area, and use a variable to dynamically change the column name in your query. example: instead of looking within title, look in description.

 

If would like to use more advanced search techniques, use a multiple query in an if/elseif/else loop. Meaning exact phrase, at least one word, must contain, begins with, etc.

 

I still recommend using the fulltext search but here is a way you can do it similar to what you are doing.

 

$sub = $_GET['article'];
$searchTerms = explode(' ', $sub);
$searchWords = array();
foreach ($searchTerms as $searchwords) {
    $searchwords = trim($searchwords);
    if (!empty($searchwords)) {
        $searchWord[] = "title LIKE '%$searchwords%'";
    }
}


$query2 = mysql_query("SELECT * FROM blog WHERE ".implode(' OR ', $searchWord).");

Link to comment
https://forums.phpfreaks.com/topic/251084-improving-my-search/#findComment-1288031
Share on other sites

There are a lot of ways to approach search; ranging from the simple MySQL LIKE to more advanced REGEX.  For full text string matching, have a look at PHP's levenstien() and metaphone() constructs. However, those constructs are expensive to call in a loop with a lot of iterations. I am a fan of getting the metaphone key value of a string and storing it along with a string; then grab the metaphone key value of the searching string and get the levenstien distal value between the search criteria and search possibility.

 

For true pattern matching, regex is hard to beat.  In an algo that I have developed before; I've used regex to isolate a possible search item, then use metaphone/levelstien to 'rank/score' the likelihood of the search possibility.

Link to comment
https://forums.phpfreaks.com/topic/251084-improving-my-search/#findComment-1288075
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.