contra10 Posted November 13, 2011 Share Posted November 13, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/251084-improving-my-search/ Share on other sites More sharing options...
QuickOldCar Posted November 14, 2011 Share Posted November 14, 2011 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)."); Quote Link to comment https://forums.phpfreaks.com/topic/251084-improving-my-search/#findComment-1288031 Share on other sites More sharing options...
phporcaffeine Posted November 14, 2011 Share Posted November 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251084-improving-my-search/#findComment-1288075 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.