Forum Moderators: coopster
I use this query on a page that allows users to select a search criteria from a list "searchtype" and submit a "searchterm" to query the database with the following query....
$query = "select * from _clientsw_cases where ".$searchtype." like '%".$searchterm."%' and ".$searchtype2." like '%".$searchterm2."%' ";
What I would do is this:
Offer the ability to the user to decide if they want to do a wild card match or exact match. You can allow them to add a * (asterisk) to their searchterm to indicate a wildcard when searching. (you could also get fancy and put a dropdown with choices like "exact match", "starts with", "ends with", "contains".
Option 1: using the * (asterisk):
When the user clicks the search button, first check to see if the searchterm is blank. If it is blank, then exit and say no results were found. This will save you returning all results in your database.
If there is a value, parse the $searchterm to see if it contains a * (asterisk). If an asterisk exists, you will use the LIKE operator and replace all asterisks in the $searchterm with a % (percent sign).
If no asterisks exist, you will use the = (equal sign).
Option 2: using the drop down
Let's call the dropdown $searchMethod
When the user clicks the search button, first check to see if the searchterm is blank. If it is blank, then exit and say no results were found. This will save you returning all results in your database.
If there is a value, check the value of the $searchMethod.
If $searchMethod = "Exact Match", you will use the equals sign.
If $searchMethod = "Starts With", you will use the LIKE operator and put a % (percent sign) at the end of the $searchTerm.
If $searchMethod = "Ends With", you will use the LIKE operator and put a % (percent sign) at the beginning of the $searchTerm.
If $searchMethod = "Contains", you will use the LIKE operator and put a % (percent sign) at the beginning and end of the $searchTerm.
Anyway, I think that should do it. You're putting the ability in the hands of the user. I like the dropdown the most because it offers the most control with the least amount of explanation.
-=casey=-
Thanks a bunch! That worked slick! Is there a way to make searchtype2 optional so if the user fills in only searchtype1 field and leaves searchtype2 field blank the query looks for that term only, but if the user fills in both fields the query looks for both search terms?
Thanks Again!
Have Several Great Days!