Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help building a query

Status
Not open for further replies.

buspass

Programmer
Feb 2, 2003
29
GB
i have a PHP search page with several text fields, surname, email,phone as an example. the user fills one or all in and clicks search button. The data is POSTed to a result.php page.

Here i need to validate what has bbeen completed, and build the query depending on which fields have been completed.

I allow the user to use the wildcard character * at the end of his text to indicate a wildcard search on that field so i have to test for the presence of a * at the end of each fielld as well.

Here is the way i do it at the moment. It works, but i get problems with results which exceed my limit which I've set to 20. I use prev and next links to display several results...

This function is included in my results.php page.

function check_star($name, $and, $part, $field)
{
global $query;
$extra = "%";
$first_char = substr(strrev($name),0,1); // reverse the string to see what first character is
if ($first_char == "*") // yes it is a wild card query
{
$rev_name = strrev(substr(strrev($name), 1, strlen($name)));// now turn string back around, and chop * off
$param = "LIKE"; // use LIKE instead of =
$name = $rev_name . $extra; // append % to the $name value
$query .= "$field $param '$name' $and";

}
else // first character is not a * so it is an exact match query
{
$param = "=";
$name = $name;
$query .= "$field $param '$name' $and";

}
}// end of function

The function is called from the results.php page for each field name with this:

if ($surname){ // the surname field is populated
if (($firstname) || ($email)){// if there is another field completed...
$and = " and ";
} else {
$and = "";
}
check_star($surname, $and, $part, "surname");
}


Hope this makes sense. If you know an easier/better method to build a query depending on a user's input, please post it.

Thanks

 
Forget this question, I've found sorted the problem out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top