Page 1 of 1

Error in search code for forum...

Posted: Fri Oct 15, 2004 5:10 pm
by Dream
It doesn't show error, it just shows all the posts instead of the ones that actually contain the search results...

The code

Code: Select all

<?php

// Words commonly used...
$look = str_replace(" a "," ",$look);
$look = str_replace(" the "," ",$look);
$look = str_replace(" and "," ",$look);

$search = explode(" ",$look);

// I want to make sure it finds a word, not part of a word...
$x = 0;
foreach($search as $searchstr){
$search[$x] = " $searchstr ";
$x = $x+1;
}

$q = "SELECT * FROM mbposts";
$do = mysql_db_query($db,$q);
while($post=mysql_fetch_array($do)){
        foreach($search as $value){
            $check = strrpos($post[post],$value);
            if($check){
                $disb = strrpos($disbe,$post[topic]);
                if(!$disb){
                $disbe.="        $post[topic]         ";
                $found = "yes";
                echo"<tr><td><a href="index.php?mode=topic&f=$post[forum]&topic=$post[topic]">$post[topic]</a></td><td>On: $post[date]</tr>";
                }
            }
        }

}

if($found!="yes"){
    echo"Sorry, no results were found";
}
?>

Posted: Fri Oct 15, 2004 5:14 pm
by John Cartwright
You do know MYSQL db's have a built in function for searching which is the easiest and fastest way to search

[devnet]+search +script[/devnet]

you should look into the "WHERE" and "LIKE" in your query so you don't have to go through each of the records and check if it matches a word.

Posted: Sat Oct 16, 2004 6:28 pm
by dethron
it will be faster if you let database does it for you ;)

Posted: Sat Oct 16, 2004 6:32 pm
by m3mn0n
I concur.

Posted: Sat Oct 16, 2004 6:34 pm
by dethron
:)

Posted: Sat Oct 16, 2004 6:38 pm
by John Cartwright
dethron wrote::)
Well this we are spamming this thread....