Results 1 to 11 of 11

Thread: Search

  1. #1

    Thread Starter
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734

    Search

    Could someone point me to an easy tutorial on making a MySQL search engine?

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    I think I a going to need some help to understand whats going on and what happens next.

  4. #4
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    The idea in that snippet is a sound one, except for the fact the search terms are hardcoded into the sql string and there are only 3 of them

    You have to build the statement on the fly, like this
    PHP Code:
    <?php
    // simple search
    // $query = search query from form
    $query 'hello world';

    $sql 'SELECT * FROM table WHERE ';

    // split into terms on space
    $terms explode(' '$query);

    // loop through array building sql statement
    for ($i=0$i count($terms); $i++) {
        
    $sql $sql."Field LIKE '%$terms[$i]%' AND ";
    }
    //remove final 'AND '
    $sql substr($sql0, -4);
    // add ending SQL
    $sql $sql.'ORDER BY Field';

    // do whatever with SQL string
    print $sql;

    ?>

  5. #5

    Thread Starter
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    this the best way to do it?

  6. #6
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    I'd be amazed to hear a better one...

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    You could also add an option to "Match Exact Text" and then do:

    PHP Code:
    <?php
    // simple search
    // $query = search query from form
    $query 'hello world';

    if (
    $_REQUEST['exact']) { //exact is the name of our option button
      
    $sql "SELECT * FROM table WHERE Field LIKE '%$query%' ";
    } else {
      
    $sql 'SELECT * FROM table WHERE ';

      
    // split into terms on space
      
    $terms explode(' '$query);

      
    // loop through array building sql statement
      
    for ($i=0$i count($terms); $i++) {
        
    $sql $sql."Field LIKE '%$terms[$i]%' AND ";
      }
      
    //remove final 'AND '
      
    $sql substr($sql0, -4);
    }

    // add ending SQL
    $sql .= 'ORDER BY Field';

    // do whatever with SQL string
    print $sql;

    ?>
    Just a thought.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8

    Thread Starter
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    Originally posted by chrisjk
    I'd be amazed to hear a better one...
    Hey, I was just asking I dont know alot about this stuff ok

  9. #9
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    if you want to do a pure Exact Match only search then you don't need to build the SQL statement on the fly at all, that's the only advantage but your users will soon get pissed off so from a usability point of view the best way IMO is to do as Hobo suggests and give the user a choice between All, Any and Exact or something

  10. #10

    Thread Starter
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    what would be cool, if you could do some sort of teir system. Like some types in "hello world". It will first find all the exact matches and print, then break "hello world" into "hello" and "world" and search and post results.

  11. #11
    scoutt
    Guest
    tht wouldn't be very good as someone will type in "the" and it will find "the" and that can return a lot of results. I would go that direction.

    beside add $query to chris's and his will do that.

Posting Permissions

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



Click Here to Expand Forum to Full Width