Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-25-2005, 03:53 AM   PM User | #1
Stevo
New to the CF scene

 
Join Date: Jan 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Stevo is an unknown quantity at this point
Showing unique search results

Hey, I can't figure out how to do something. I have a script that searches a table and returns each entry that contains one or more of the keywords. The problem is that it shows the same result for each keyword that shows up. I want each entry to only show up once. I'm not sure what the best way to do this is. Also, is there any way to sort the results depending on how many keywords show up in an entry?

Here's the script I have so far:
PHP Code:
<?php
include("config.php");
include(
"layout_header.php");

echo 
"<table class=\"table\" cellspacing=\"0\"><tr class=\"title\"><td>Search</td></tr>\n";
echo 
"<tr class=\"content\"><td>\n";

if(!
$_GET['keywords']){

echo 
"<form action=\"$PHP_SELF\" method=\"get\">\n";
echo 
"<input type=\"text\" name=\"keywords\">\n";
echo 
"<input type=\"submit\">\n";
echo 
"</form>\n";

}else{

$keywords_array explode(" ",$_GET['keywords']);

foreach(
$keywords_array as $keyword){

$query mysql_query("SELECT * FROM `hlcd_clans` WHERE `name` OR `description` LIKE '%$keyword%'");

if(
mysql_numrows($query) < 1){
echo 
"Your search returned no results. <a href=\"$PHP_SELF\">Back</a>\n";
}

while(
$myrow mysql_fetch_array($query)){

$id $myrow['id'];
$name $myrow['name'];
$description $myrow['description'];

echo 
"<a href=\"listing.php?id=$id\">$name</a><br>$description<br><br>\n";
}

}
}

echo 
"</td></tr></table>\n";

include(
"layout_footer.php");
?>
Any help would be appreciated.
Stevo is offline   Reply With Quote
Old 01-25-2005, 11:16 AM   PM User | #2
Sayonara
Regular Coder

 
Sayonara's Avatar
 
Join Date: Oct 2004
Location: UK
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Sayonara is an unknown quantity at this point
I am not sure what it is you want to make unique. Do you mean for each keyword, you want only one result returned, or each result should only be returned once regardless of how many keyword matches there are?

For the second part of your question, you are delivering your search results into an array. You could therefore use some string matching to reorder the array based on the keywords.
__________________
Quote:
Originally Posted by liorean
Just remember that you code for the user, and the user visits you because what you code is good.
Sayonara is offline   Reply With Quote
Old 01-25-2005, 07:59 PM   PM User | #3
Stevo
New to the CF scene

 
Join Date: Jan 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Stevo is an unknown quantity at this point
I want each result to only show up once regardless of how many keywords there are.
Stevo is offline   Reply With Quote
Old 01-25-2005, 08:37 PM   PM User | #4
AaronW
Senior Coder

 
Join Date: Feb 2003
Location: Ontario, Canada
Posts: 1,223
Thanks: 0
Thanked 0 Times in 0 Posts
AaronW is an unknown quantity at this point
You're running a query inside a loop. That's rarely a good idea.

Try this:
PHP Code:
<?
  
...
  
$query "SELECT * FROM hlcd_clans WHERE";

  foreach (
$_GET['keywords'] as $keyword)
    
$query .= " (name LIKE '%".$keyword."%' OR description LIKE '%".$keyword."%') OR";

  
$result mysql_query (substr ($query0, -3));
  ...
?>
Then $result will hold all the rows and you can loop through it using mysql_fetch_assoc as usual. One query.
__________________
offtone.com | offtonedesign.com

Last edited by AaronW; 01-25-2005 at 08:40 PM..
AaronW is offline   Reply With Quote
Old 01-26-2005, 08:04 AM   PM User | #5
Sayonara
Regular Coder

 
Sayonara's Avatar
 
Join Date: Oct 2004
Location: UK
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Sayonara is an unknown quantity at this point
Snappy
__________________
Quote:
Originally Posted by liorean
Just remember that you code for the user, and the user visits you because what you code is good.
Sayonara is offline   Reply With Quote
Old 01-28-2005, 03:52 AM   PM User | #6
Stevo
New to the CF scene

 
Join Date: Jan 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Stevo is an unknown quantity at this point
I got it working. Thanks for the help!
Stevo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:19 PM.


Advertisement
Log in to turn off these ads.