Error in search code for forum...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Dream
Forum Newbie
Posts: 17
Joined: Sat Aug 16, 2003 7:15 pm
Location: Norway
Contact:

Error in search code for forum...

Post 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";
}
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

it will be faster if you let database does it for you ;)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

I concur.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

:)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

dethron wrote::)
Well this we are spamming this thread....
Post Reply