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 3 of 3
  1. #1
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts

    Comparing Arrays

    I have a comma delimited column for keywords in a DB. I was wondering the best way to compare these keywords with user submitted search terms.

    I was thinking about putting the user entered text in an array and then comparing that array for each keyword array in the DB.

    Other than me having a hard time with accomplishing this...I don't think this is the most efficient way of going about this.
    This is what I have for parsing the user input search terms:

    PHP Code:
    $input basicClean($_GET['input']); //some basic string cleaning
    $input preg_replace('/[^a-z0-9&\s]/i'''$input); //only allows alphanumeric characters, ampersand (&), and spaces.
    $input trim($input); // trims beginning and trailing space from text.
    $input strtolower($input); //makes the text lowercase
    $input explode(" ",$input);  //creates an array delimited by spaces
    $input array_filter($input); //removes emptry strings, null values, and values that equate to false
    $input array_merge(array_flip(array_flip($input))); //removes duplicate entries 
    I just didn't know what to do beyond this to be able to compare to the keyword array for each row in the DB and rank each.

    I'm open to any suggestions whether its a 3rd party class script or totally different approach.

    Edit: I wanted to add that the keywords are not for actual pages but for business locations. The pages are created dynamically. So I would need to make a reference for each location ID.
    Last edited by StupidRalph; 01-29-2007 at 01:38 PM. Reason: Additional information
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Okay so with a little help of a foreach statement and the in_array function I am able to test check the user entered data against whats in the database and count the number of occurences.

    PHP Code:
    $_GET['input'] = "Income Tax Rapid Refund";  // Example of what a user may enter as search text.
    $input basicClean($_GET['input']); //some basic string cleaning 
    $input preg_replace('/[^a-z0-9&\s]/i'''$input); //only allows alphanumeric characters, ampersand (&), and spaces. 
    $input trim($input); // trims beginning and trailing space from text. 
    $input strtolower($input); //makes the text lowercase 
    $input explode(" ",$input);  //creates an array delimited by spaces 
    $input array_filter($input); //removes emptry strings, null values, and values that equate to false 
    $input array_merge(array_flip(array_flip($input))); //removes duplicate entries  and reindexes the keys

    $haystack explode(",""money,managers,accounting,rapid,refund"); //mock example of keywords array from database.

    $occurences 0// init variable for how many times the keywords 
    foreach ($input as $needle) { 
        
        if (
    in_array($needle,$haystack)) { //check to see if the input keyword is in the DB keyword array.
            
    $occurences++; //its here so add one for occurence    
        
    }
        
    }
    echo 
    "there are " $occurences " occurences of matching keywords"
    So I'm guessing my next step is to put the ID for each row in an array along w/ the number of occurences and then do a sort on the occurences.
    Last edited by StupidRalph; 01-29-2007 at 01:49 PM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #3
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Seek and thou shall find...

    http://dev.mysql.com/doc/refman/4.1/...xt-search.html
    This is sooooooooo much easier lol.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

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