...

View Full Version : Internal Search Engine tutorials



2ksports
06-09-2006, 10:54 PM
I'm looking for any good tutorials on building a search function for a primarily database driven site. An article that has tips on how to get the most relevant content to display first is my primary concern.

Thanks for any help!

lansing
06-10-2006, 01:19 PM
Here is a very basic search box for your site. In the drop down menu make the Value of each entry the Column name in your MySQL DB. If have several DB's you want to search then just add another menu & make the value the table name. This isn't the best search script for your site, but it will get you started.


<div align="center">
<?PHP
if($_POST['search1'])
{
trim($_POST["searchterm"]);
if (!($_POST["searchtype"]) || !($_POST["searchterm"]))
{
echo "You have not entered search details. Please go back and try again.";
exit;
}

$searchtype = mysql_escape_string($_POST["searchtype"]);
$searchterm = mysql_escape_string($_POST["searchterm"]);

$query = "SELECT * FROM table WHERE ".$searchtype." LIKE '%".$searchterm."%'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);

echo "<p>Number of entries found: ".$num_results."</p>";

echo '<TABLE border="0">';
echo '<TR><TH>First:</TH><TH>Last:</TH><TH>E-Mail</TH><TH>Phone</TH><TH>Phone 2</TH><TH>Street</TH>
<TH>City</TH><TH>State</TH><TH>Zip</TH><TH>Country</TH></TR>';
$bgcolor = "#FFFFFF";
while ( $row = mysql_fetch_array( $result ) )
{
if ($bgcolor == "#FFFFFF"){
$bgcolor = "#CACACA";
}else{
$bgcolor = "#FFFFFF";
}

echo '<TR bgcolor="'.$bgcolor.'">';
echo '<TD>' . htmlspecialchars( stripslashes( $row['id'] ) ) . '</TD>';
echo '<TD>' . htmlspecialchars( stripslashes( $row['firstname'] ) ) . '</TD>';
echo '<TD>' . htmlspecialchars( stripslashes( $row['lastname'] ) ) . '</TD>';
echo '<TD>' . htmlspecialchars( stripslashes( $row['email'] ) ) . '</TD>';
echo '<TD>' . htmlspecialchars( stripslashes( $row['phone'] ) ) . '</TD>';
echo '<TD>' . htmlspecialchars( stripslashes( $row['phone2'] ) ) . '</TD>';
echo '<TD>' . htmlspecialchars( stripslashes( $row['street_address'] ) ) . '</TD>';
echo '<TD>' . htmlspecialchars( stripslashes( $row['city'] ) ) . '</TD>';
echo '<TD>' . htmlspecialchars( stripslashes( $row['state'] ) ) . '</TD>';
echo '<TD>' . htmlspecialchars( stripslashes( $row['postcode'] ) ) . '</TD>';
echo '<TD>' . htmlspecialchars( stripslashes( $row['country'] ) ) . '</TD>';
echo '</TR>';
}

echo '</TABLE>';
}
else
{
?>

<h1><font face="Arial, Helvetica, sans-serif" color="#333333">Customer Search</font></h1>

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<table width="40%" border="0">
<tr>
<td><div align="center"><font face="Arial, Helvetica, sans-serif" color="#333333" size="2">Choose Search Type:</font><br>
<select name="searchtype">
<option>Choose customer criteria</option>
<option value="id">Customer ID</option>
<option value="firstname">First Name</option>
<option value="lastname">Last Name</option>
<option value="email">E-Mail</option>
<option value="phone">Phone</option>
<option value="phone2">Mobile</option>
<option value="city">City</option>
<option value="state">State</option>
<option value="postcode">Zip</option>
<option value="country">Country</option>
</select>
<br>
</div></td>
<td><div align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#333333">Enter Search Term:</font><br>
<input name="searchterm" type="text">
</div></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="search1" type=submit id="search1" value="Search">
</div></td>
</tr>
</table>
</form>
<?
}
?>

</div>

vinyl-junkie
06-10-2006, 06:26 PM
You might want to read this article (http://moskalyuk.com/php/yourownsearchengine.htm), which walks you through how to build a basic site search engine. It includes relevancy of search results. Excellent stuff!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum