Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Senior Member
    Join Date
    Aug 2011
    Posts
    227
    Member #
    29153
    Liked
    4 times
    How do I search a database for example, if someone puts in "loose foot", it would return any instance of the word foot with the word loose. If they search loose foot, and foot loose is in the database, it would show up in the search.

  2.  

  3. #2
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    Where [Field] like ''%foot%' and [Field] Like '%loose%'

    The exact formatting of your where clause will depend on your database, but this will give you the general idea. You'd have to split your query using a server-side programming language, or in the case of SQL Server the readily available Split function, but it will work.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  4. #3
    Senior Member
    Join Date
    Aug 2011
    Posts
    227
    Member #
    29153
    Liked
    4 times
    I'm using php & MySQL

  5. #4
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    I wouldn't really be able to help you other than in concept, then. You'd have to find a PHP split function in all likelihood and then format your query accordingly for MySQL.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  6. #5
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    Ideally you'd use something like a Lucene index for this type of thing. However as TheGAME1264 suggests you can perform searches like this with MySQL. Though this would need to match every word

    Something like this would do the trick: (Not completely tested)

    Just put in a query string like ?s=this+is+a+test to the page

    PHP Code:
    <?php

    /*******************************************
    Example Class to create loose keyword style
    searched with MySQL.
    *******************************************/

    class Keyword_Search_Model {

    private 
    $search;
    private 
    $fieldName;
    private 
    $tableName;
    private 
    $queryConditions;

    public function 
    __construct() {
    $this->search $this->findSearchTerms();
    }

    private function 
    createQueryConditions() {
    if (isset(
    $this->fieldName) && $this->fieldName != NULL) {
    $query_words explode(" "$this->search); //Explode into array by space
    $array_length count($query_words); //Get count of array

    $condition "WHERE "$this->fieldName" LIKE ";
    $i 0;
    foreach(
    $query_words as $word) {
    $condition .= "'%"$word"%' ";
    if(
    $array_length != ($i 1)) { //If it's not the last iteration of the array
    $condition.= "AND "$this->fieldName" LIKE ";
    }
    $i++;
    }
    $this->queryConditions $condition;
    }
    else {
    throw new 
    Exception('Field Name Not Set.');
    }
    }

    private function 
    findSearchTerms() {
    return (isset(
    $_GET['s']) ? $_GET['s'] : NULL); //Get your query string
    }

    public function 
    getSearchResults() {

    if(
    $this->search != NULL) {
    $this->createQueryConditions();
    if(isset(
    $this->tableName) && $this->tableName != NULL) {
    $toReturn = array();
    $sql "SELECT FROM "$this->tableName " "$this->queryConditions;
    echo 
    $sql"<br />"//Use this to see the string that would appear! Comment out to remove
    $result mysql_query($sql);

    if(
    $result != 0) {
    while (
    $row mysql_fetch_object($result)) {
    $toReturn[] = $row;
    }
    return 
    $toReturn//Return
    }
    else {
    echo 
    "No results found";
    }
    }
    else {
    throw new 
    Exception('Table Name Not Set.');
    }
    }
    else {
    echo 
    "No search to query.<br />";
    }
    }

    /*******************************************
    Getters and Setters

    @param string
    @return void
    *******************************************/

    public function setFieldName($field_name) {
    $this->fieldName $field_name;
    }

    public function 
    setTableName($table_name) {
    $this->tableName $table_name;
    }
    }

    /*******************************************

    Example Usage

    *******************************************/

    $keyword_search = new Keyword_Search_Model;
    $keyword_search->setFieldName('content');
    $keyword_search->setTableName('blog');
    echo 
    $keyword_search->getSearchResults();

    ?>
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:51 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com