PDA

View Full Version : PHP Site Search



webmasterjunkie
12-03-2004, 04:29 PM
Hi All,

I just finished my "beta" site search for one of the sites I maintain. I would appreciate it if you all could check it out and let me know what you think.

You have to go to the site map, http://www.homesearch-md.com/site_map.html to use the search, and it's on the bottom of the page. Below is the php source that searches my database and arranges the results.

I am looking for feedback on the results mainly, so anything you have to say works.

<?
$body_text = "";
$search_complete = "no";
$connection = @mysql_connect("host","username","password") or die (mysql_error());
$db_name = "mydatabase";
$search_text = $_POST['search_for'];
if ($search_text != "") {
echo "</head><body>";
echo "$body_text";
$search_result = "";
while ($search_complete == "no") {
$table_name = "pages";
$search_by = "body_text";
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT * FROM $table_name WHERE $search_by LIKE \"%%$search_text%%\"";
$result = @mysql_query($sql, $connection) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$url_link = $row['url_link'];
$url_title = $row['url_title'];
$description = $row['description'];
$body_words = $row['body_text'];
$keywords = $row['keywords'];
if ($search_text == $keywords && $search_text == $url_title && $search_text == $description) {
$search_result .= "<font face=\"Arial\">
<a href=\"$url_link\">$url_title</a>
$description

</font>";
}
if ($search_text == $keywords && $search_text == $description) {
$search_result .= "<font face=\"Arial\">
<a href=\"$url_link\">$url_title</a>
$description

</font>";
}
if ($search_text == $keywords) {
$search_result .= "<font face=\"Arial\">
<a href=\"$url_link\">$url_title</a>
$description

</font>";
}
if ($search_text == $description) {
$search_result .= "<font face=\"Arial\">
<a href=\"$url_link\">$url_title</a>
$description

</font>";
} else {
$search_result .= "<font face=\"Arial\">
<a href=\"$url_link\">$url_title</a>
$description

</font>";
}
}
$search_complete = "yes";
}
if ($search_result == "") {
$search_result = "
Sorry, No Matches. Try the <a href=\"http://www.homesearch-md.com/site_map.html\">Site Map</a>";
}
if ($search_complete == "yes") {
$body_text .= "$search_result";
}
}
?>

webmasterjunkie
12-04-2004, 10:36 AM
<?
$body_text = "";
$page = "";
$search_complete = "no";
$search_result = "";
$omitted_search_result = "";
$search_result_likely = "";
$search_text = $_POST['search_for'];
$connection = @mysql_connect("myhost","myuser","mypassword") or die (mysql_error());
$db_name = "homesear_search";
if ($search_text != "") {
echo "</head><body>";
while ($search_complete == "no") {
$table_name = "pages";
$search_by = "body_text";
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT * FROM $table_name WHERE $search_by LIKE \"%%$search_text%%\"";
$result = @mysql_query($sql, $connection) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$url_link = $row['url_link'];
$url_title = $row['url_title'];
$description = $row['description'];
$body_words = $row['body_text'];
$keywords = $row['keywords'];
if ($keywords >= $search_text) {
if ($description >= $search_text) {
if ($url_title >= $search_text) {
if ($url_link >= $search_text) {
$search_result_likely .= "<font face=\"Arial\">
<a href=\"links.php?id=$id&keywords=$search_text\">$url_title</a>
$description</font>

<font color=\"#008080\" size=\"2\">$url_link</font>

";
} else {
$search_result_likely .= "<font face=\"Arial\">
<a href=\"links.php?id=$id&keywords=$search_text\">$url_title</a>
$description</font>

<font color=\"#008080\" size=\"2\">$url_link</font>

";
}
} else {
$search_result_likely .= "<font face=\"Arial\">
<a href=\"links.php?id=$id&keywords=$search_text\">$url_title</a>
$description</font>

<font color=\"#008080\" size=\"2\">$url_link</font>

";
}
} else {
$search_result_likely .= "<font face=\"Arial\">
<a href=\"links.php?id=$id&keywords=$search_text\">$url_title</a>
$description</font>

<font color=\"#008080\" size=\"2\">$url_link</font>

";
}
} else {
$omitted_search_result .= "<font face=\"Arial\">
<a href=\"links.php?id=$id&keywords=$search_text\">$url_title</a>
$description</font>

<font color=\"#008080\" size=\"2\">$url_link</font>

";
}
$search_result = "$search_result_likely $omitted_search_result";
}
$search_complete = "yes";
}
if ($search_result == "") {
$search_result = "
Sorry, No Matches. Try the <a href=\"http://www.homesearch-md.com/site_map.html\">Site Map</a>";
}
if ($search_complete == "yes") {
$body_text .= "$search_result";
}
}