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
Dream
Forum Newbie
Posts: 17 Joined: Sat Aug 16, 2003 7:15 pm
Location: Norway
Contact:
Post
by Dream » Fri Oct 15, 2004 5:10 pm
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";
}
?>
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Fri Oct 15, 2004 5:14 pm
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.
dethron
Forum Contributor
Posts: 370 Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul
Post
by dethron » Sat Oct 16, 2004 6:28 pm
it will be faster if you let database does it for you
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Sat Oct 16, 2004 6:32 pm
I concur.
dethron
Forum Contributor
Posts: 370 Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul
Post
by dethron » Sat Oct 16, 2004 6:34 pm
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sat Oct 16, 2004 6:38 pm
dethron wrote:
Well this we are spamming this thread....