Is it possible to create a search that allows you to identify which field of a database you want to search through. I already created a working search that looks through the field name and produces the results i need. Is there some easy way to adapt this to my needs or should i start tampering with it myself and do another 2 hour trial and error. Id like to set a variable to which field it searches through and have that be user defined while someone is using the site. Here is what i have coded so far. The field is defined at the very top of the document
Code:
<?php
// get the query for the search engine
$var = @$_GET['q'] ; (if applicable)
//trim whitespace from the stored
$trimmed = trim($var); variable
include 'include/include_dbconnect.php';
ConnectToDatabase();
[COLOR=red]$field_to_search = "name";
$query = "SELECT * FROM $dbtable WHERE $field_to_search LIKE \"%$trimmed%\" order by id";[/color]
include 'include/include_common.php';
?>
<html>
<head>
<title><?=$company?> - <?=$appName?></title>
</head>
<body <?=$bodyTagAttribs?> >
<center>
<?php
// Put in the header //
include '../includes/include_header.php';
// Print top caption for table
echo "<br><CAPTION ALIGN=top><FONT size=4 color=\"white\">Insert information about this site later.</FONT></CAPTION><br>";
// Begin Search Engine Code
print("<form name=\"search\" method=\"GET\" action=\"$PHP_SELF\">");
print("<font color=\"white\">Seach the Database for: </font>");
print("<input type=\"text\" name=\"q\">");
print("<input type=\"submit\" name=\"search\" value=\"Search\">");
print("</form>");
if ($trimmed == "")
{
echo "<p><font color=\"white\">Please enter a search</font></p>";
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "<p><font color=\"white\">We dont seem to have a search parameter!</font></p>";
exit;
}
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p><font color=\"white\">Sorry, your search: " . $trimmed . " returned zero results</font></p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s))
{
$s=0;
}
// get results
$result = mysql_query($query) or die("<font color=\"white\">Couldn't execute query</font>");
if($numrows > 1){ $return = "results";}
else{ $return = "result"; }
// display what the person searched for
echo "<p><font color=\"white\">Your search for " . $var . " returned $numrows $return.</font></p>";
// begin to show results set
$count = 1 + $s ;
//Sets up table format
echo "<table WIDTH=100% BORDER=\"1\" CELLPADDING=\"1\" bgcolor=\"" . $gridBGColor1 . "\">";
echo "<tr><th>Name</th><th>Month</th><th>Day</th><th>Year</th><th>Hours</th><th>Project ID</th><th>Description / Comments<th></tr>\n";
while ($r= mysql_fetch_array($result))
{
$name = $r["name"];
$month = $r["month"];
$day = $r["day"];
$year = $r["year"];
$hours = $r["hours"];
$pid1 = $r["pid1"];
$pid2 = $r["pid2"];
$comment = $r["comment"];
$count++ ;
// Sets up format for data presentation
printf("<tr><td valign=\"top\">%s</td><td valign=\"top\">%s</td><td valign=\"top\">%s</td><td valign=\"top\">%s</td><td valign=\"top\">%s</td><td width=75 valign=\"top\">%s-%s</td><td>%s</tr>\n", $r["name"], $r["month"], $r["day"], $r["year"], $r["hours"], $r["pid1"], $r["pid2"], $r["comment"]);
}
echo "</table>\n";
// Put in Footer
include '../includes/include_footer.php';
?>