Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    256
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I want each result to only show up once regardless of how many keywords there are.

  • #4
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by AaronW; 01-25-2005 at 08:40 PM.

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Posts
    256
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Snappy

  • #6
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I got it working. Thanks for the help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •