Hi there,
I’ve written the following code (with some help via online tutorials) to facilitate a “name finder” for a website I am developing. The user will search for a name, and the form will return results showing the name’s origin and meaning.
What I’m missing is the ability to narrow down the search by Gender. I want the users to be able to select whether they are searching for a Male name or a Female name. The “gender” field is in the same table.
So basically, I need the HTML form to have a drop down box with Male & Female as the options and whichever one is picked needs to be included in the search as the “gender” variable… I hope that makes sense.
Can someone help?
// Set up our error check and result check array
$error = array();
$results = array();
// First check if a form was submitted.
// Since this is a search we will use $_GET
if (isset($_GET['search'])) {
$searchTerms = trim($_GET['search']);
$searchTerms = strip_tags($searchTerms); // remove any html/javascript.
if (strlen($searchTerms) < 3) {
$error[] = "Search terms must be longer than 3 characters.";
}else {
$searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
}
// If there are no errors, lets get the search going.
if (count($error) < 1) {
$searchSQL = "SELECT name, gender, meaning, origin FROM names WHERE ";
// grab the search types.
$types = array();
$types[] = isset($_GET['name'])?"`name` LIKE '{$searchTermDB}'":'';
$types[] = isset($_GET['gender'])?"`gender` LIKE '{$searchTermDB}'":'';
$types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
if (count($types) < 1)
$types[] = "`name` LIKE '{$searchTermDB}'"; // use the name as a default search if none are checked
$andOr = isset($_GET['matchall'])?'AND':'OR';
$searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `name`"; // order by title.
$searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
if (mysql_num_rows($searchResult) < 1) {
$error[] = "The search term provided {$searchTerms} yielded no results.";
}else {
$results = array(); // the result array
$i = 1;
while ($row = mysql_fetch_assoc($searchResult)) {
$results[] = "{$i}: Name: <span id=\\"result\\">{$row['name']}</span><br /> Gender: <span id=\\"result\\">{$row['gender']}</span><br /> Origin: <span id=\\"result\\">{$row['origin']}</span><br /> Meaning: <span id=\\"result\\">{$row['meaning']}</span><br /><br />"; $i++;
}
}
}
}
function removeEmpty($var) {
return (!empty($var));
}
?>
<html>
<title>Name Finder</title>
<style type="text/css">
#error {
color: red;
}
#terms {
color: red;
text-decoration: underline;
font-weight: bold;
}
#result {
color: blue;
font-weight: bold;
}
</style>
<body>
<?php echo (count($error) > 0)?"The following had errors:<br /><span id=\\"error\\">" . implode("<br />", $error) . "</span><br /><br />":""; ?> <form method="GET" action="<?php echo $_SERVER['../../../PHP_SELF'];?>" name="searchForm"> <strong>Enter a name to find it's origin and meaning: </strong> <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /> <br /><br />
<input type="submit" name="submit" value="Search!" />
</form>
<?php echo (count($results) > 0)?"You searched for: <span id=\\"terms\\">{$searchTerms}</span> - Here are your results:<br /><br />" . implode("", $results):""; ?>
</body>
</html>