Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: [MOD] Change to search function  (Read 2049 times)

mattd

  • Full Member
  • ***
  • Karma: 0
  • Posts: 121
[MOD] Change to search function
« on: March 11, 2006, 02:36:14 PM »

Hi,

Made a little change to the seach function so it will search for multiple keywords, e.g. if you put in "blue red" into the search box it will find articles with "blue", articles with "red" and articles with both "blue" and "red" in it, and not exact phrase like the default.

Might be useful for some of you (its live on my site (link in sig) if you want to testdrive first):

Code: [Select]
// SEARCH ENGINE
function search() {
$search_query = clean($_POST['search_query']);
$search_query_var = "%".$search_query."%";
echo "

". l(search_results) ."

";
if (strlen($search_query) < 3) {
echo "

". l(charerror) ."

";
} else {
        // Matt 11/3/06 13:24 - build up the SQL query so we can use multiple keywords
        $keywords = explode(" ",$search_query);        
        $query = "SELECT * FROM " .s('prefix'). "articles WHERE ";
        if (count($keywords) >= 2) {
            for ($i=0;$i                $query = $query . " (title LIKE '%$keywords[$i]%' || text LIKE '%$keywords[$i]%') ||";
            }
            $j = count($keywords)-1;
            $query = $query . " (title LIKE '%$keywords[$j]%' || text LIKE '%$keywords[$j]%')";
        } else {
            $query = $query . " (title LIKE '%$keywords[0]%' || text LIKE '%$keywords[0]%')";
        }
        $query = $query . " AND position <> 2 AND published = 1 ORDER BY id DESC";
        // ttaM        
        $result = mysql_query($query);
while ($r = mysql_fetch_array($result)) {
$num++;
$date = date(s('date_format'), strtotime($r['date']));
echo "

" .$r['title']. " > " . $date. "

";
}
if ($num == "") { echo "

". l('noresults') ." " . $search_query . ".

";
$num = "0";
} else { echo "

" . $num . " ". l('resultsfound') ." " . $search_query . ".

"; }}

echo "


". l('backhome') ."

";
}
For those interested, this just replaces the one line of code where the SQL query was generated.  All this does is break the query into an array, then build up a new SQL query with multiple LIKE bits.

Oh and I changed the minimum characters to anything less than 3 because there are a load of 3 lettered acronyms like SQL, ASP, JSP, PHP etc etc that I want people to be able to search by  :D

Matt
Logged

witit

  • Newbie
  • *
  • Karma: 1
  • Posts: 18
Re: [MOD] Change to search function
« Reply #1 on: April 05, 2012, 05:26:53 AM »

There is an error:
Quote
Parse error: syntax error, unexpected T_VARIABLE, expecting ';'

around this line:

Quote
if (count($keywords) >= 2) {
            for ($i=0;$i                $query = $query . " (title LIKE '%$keywords[$i]%' || text LIKE '%$keywords[$i]%') ||";
            }

I'm using 1.7.1
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: [MOD] Change to search function
« Reply #2 on: April 05, 2012, 05:30:50 AM »

witit, you do know this is for a different version that isn't compatible with this mod....
Logged

witit

  • Newbie
  • *
  • Karma: 1
  • Posts: 18
Re: [MOD] Change to search function
« Reply #3 on: April 05, 2012, 05:37:55 AM »

Just noticed that. I came from a google search. Thanks anyway.
Logged