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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Search Engine to Find Exact Matches

    Hi Guys!

    First off I am a bit of a PHP / MySQL novice, so please go gentle!

    I'm creating a site for my friends business with motorcycle part diagrams and part numbers so they can look up items without going through manuals etc.

    I want to add a part number search facility so that they can find any motorcycles that list this part as well.

    So far I have used a open-source script to get the search part working, however when I enter a part number, it displays all of the pages it has indexed.

    MySQL has one table (called websites) with three columns - URL, title & description...
    URL: Website link to the page
    Title: Title of the page
    Description: A list of part numbers used within this page

    EXAMPLE:

    URL: http://www.website.com/page1.php
    Title: Page 1
    Description: 12345, 67890, 09876, 54321

    This is the query that I am firing at the database when a part number is entered into the search box.

    Code:
    <?
    include "config.php";
    if (@$_POST['search']=="search")
    {
    $keyword=$_POST['Number'];
    $result=mysql_query("SELECT * FROM websites WHERE `title` LIKE '%$Number%' OR `url` LIKE '%$Number%' OR `description` LIKE '%$Number%' ");
    while ($results = mysql_fetch_array($result))
    {
    $title=$results['title'];
    $url=$results['url'];
    $description=$results['description'];
    echo "<ul><li><a href='$url'>$title</a></li></ul><br>";
    }
    }
    ?>
    Is there a way I can make it look for exact matches within the description and only show them in the results, instead of all of the pages?

    Thanks

    Gazza

  • #2
    Regular Coder
    Join Date
    Dec 2007
    Posts
    137
    Thanks
    1
    Thanked 21 Times in 21 Posts
    I think you have a typo, no?

    You are searching for anything that matches the $Number variable, but you have not defined $Number , so it's undefined. Therefore your SQL query is returning EVERYTHING that matches "", an empty string.

    I think you need to use the $keyword variable in your SQL query string, since you already have it declared it in your line:

    Code:
    $keyword=$_POST['Number'];
    So you want to use that $keyword variable (not $Number) in your SQL query, because it holds the number value that your got from the POST request object.

    Just do a simple replace of '$Number' with '$keyword' in your mysql_query line.

    Replace this line:
    Code:
    $result=mysql_query("SELECT * FROM websites WHERE `title` LIKE '%$Number%' OR `url` LIKE '%$Number%' OR `description` LIKE '%$Number%' ")
    With this line:
    Code:
    $result=mysql_query($conn, "SELECT * FROM websites WHERE `title` LIKE '%$keyword%' OR `url` LIKE '%$keyword%' OR `description` LIKE '%$keyword%' ");
    You should get the results that you want now.

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, seems to be working better, however....

    When I search for a part number that begins with the same as another part on a different page, it returns no results...

    Example:

    URL: http://www.website.com/page1.php
    Title: Page 1
    Description: 12345, 67890, 09876, 54321

    URL: http://www.website.com/page2.php
    Title: Page 2
    Description: 12354, 67809

    If I search for 12345, it returns no results, and searching 123 again returns no results. However if I search 09876 it shows Page 1 with no issues.

    I've changed my code to the following:

    Code:
    <?
    include "config.php";
    if (@$_POST['search']=="search")
    {
    $keyword=$_POST['keyword'];
    $result=mysql_query("SELECT * FROM websites WHERE `title` LIKE '%$keyword%' OR `url` LIKE '%$keyword%' OR `description` LIKE '%$keyword%' ");
    while ($results = mysql_fetch_array($result))
    {
    $title=$results['title'];
    $url=$results['url'];
    $description=$results['description'];
    echo "<ul><li><a href='$url'>$title</a></li></ul><br>";
    }
    }
    ?>
    Any ideas?

  • #4
    Regular Coder
    Join Date
    Dec 2007
    Posts
    137
    Thanks
    1
    Thanked 21 Times in 21 Posts
    Hmm... maybe there are whitespace characters in your string.

    Try trimming the value of 'Numbers' that you are getting from your form.

    Replace this line:
    Code:
    $keyword=$_POST['keyword'];
    With this:
    Code:
    $keyword=trim($_POST['Number']);
    If you type in "123 " or " 123", it should get rid of the leading or ending spaces and just transform it to "123", whereas before it would not and look for "123 " and return no results since it'll also look for the spaces.

    See if that helps.

  • #5
    Regular Coder
    Join Date
    Dec 2007
    Posts
    137
    Thanks
    1
    Thanked 21 Times in 21 Posts
    Oh yeah, I noticed your were using POST['keyword']; just now.

    I assumed your were using 'Number' as the POST value. If not, then use
    Code:
    $keyword=trim($_POST['keyword']);
    But just to clarify, in your form that has your parts textbox, is the textbox's input name set as 'keyword' or as 'Number'? I assumed 'Number', based on your first code, but your most recent suggests that your form is using 'keyword' instead.

    Can you post your FORM html code that is getting the part number?


  •  

    Tags for this Thread

    Posting Permissions

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