hello,
my request in detail for the search program is:
search code for query term to produce its link e.g: when the user write in the inbox search (internet) the program is searching in the database in the keyword ,title and description fields ..>if it is found the result is: URL which have the word (internt) ...>else the result is:sorry the word is not found
this is for one word...but for many word..
so i'm writing code take it from site ,but this code must change in order to become suitable for my request..
my tables in the database "search_engine_db" are :
1- web_page (have the columns "URL","title","description")
2- keyword (have the columns "URL","keyword")
3- link (have the columns "URL","link")
all of these tables are fulling by crawler program
<html>
<head><title>Search</title></head>
<body>
<?php
// Full-Text Search Example
// Conect to the database.
$cnx = mysql_connect('localhost', 'phpfreaks', 'phpfreaks') or die ("Could not connect");
mysql_select_db('phpfreaks_search', $cnx) or die (mysql_error());
// Create the search function:
function searchForm()
{
// Re-usable form
// variable setup for the form.
$searchwords = (isset($GET['words']) ? htmlspecialchars(stripslashes($REQUEST['words'])) : '');
$normal = (($GET['mode'] == 'normal') ? ' selected="selected"' : '' );
$boolean = (($GET['mode'] == 'boolean') ? ' selected="selected"' : '' );
echo '<form method="get" action="'.$_SERVER['PHP_SELF'].'">';
echo '<input type="hidden" name="cmd" value="search" />';
echo 'Search for: <input type="text" name="words" value="'.$searchwords.'" /> ';
echo 'Mode: ';
echo '<select name="mode">';
echo '<option value="normal"'.$normal.'>Normal</option>';
echo '<option value="boolean"'.$boolean.'>Boolean</option>';
echo '</select> ';
echo '<input type="submit" value="Search" />';
echo '</form>';
}
// Create the navigation switch
$cmd = (isset($GET['cmd']) ? $GET['cmd'] : '');
switch($cmd)
{
default:
echo '<h1>Search Database!</h1>';
searchForm();
break;
case "search":
searchForm();
echo '<h3>Search Results:</h3><br />';
$searchstring = mysql_escape_string($_GET['words']);
switch($_GET['mode'])
{
case "normal":
$sql = "SELECT mytable_id, mytable_title, mytable_caption, mytable_dts,
MATCH(mytable_title, mytable_caption, mytable_full_body)
AGAINST ('$searchstring') AS score FROM mytable
WHERE MATCH(mytable_title, mytable_caption, mytable_full_body)
AGAINST ('$searchstring') ORDER BY score DESC";
break;
case "boolean":
$sql = "SELECT mytable_id, mytable_title, mytable_caption, mytable_dts,
MATCH(mytable_title, mytable_caption, mytable_full_body)
AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM mytable
WHERE MATCH(mytable_title, mytable_caption, mytable_full_body)
AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC";
break;
}
// echo $sql;
$result = mysql_query($sql) or die (mysql_error());
while($row = mysql_fetch_object($result))
{
echo '<strong>Title: '.stripslashes(htmlspecialchars($row->mytable_title)).'</strong><br />';
echo 'Score:'. number_format($row->score, 1).' Date: '.date('m/d/y', $row->mytable_dts).'<br />';
echo '<p>'.stripslashes(htmlspecialchars($row->mytable_caption)).'</p>';
echo '<hr size="1" />';
}
break;
}
?>
</body>
</html>
also i have another code but i don't know is it the best:
+++++++++++++++++++++++++++++++++++++++++++++++++
<html>
<head>
<title>designplace.org search script</title>
<meta name="author" content="Steve R, http://www.designplace.org/">
</head>
<!-- © http://www.designplace.org/ -->
<body>
<form name="form" action="search.php" method="get">
<input type="text" name="q" />
<input type="submit" name="Submit" value="Search" />
</form>
<?php
// Get the search variable from URL
$var = @$_GET['q'] ;
$trimmed = trim($var) //trim whitespace from the stored variable
// rows to return
$limit=10;
// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to your database EDIT REQUIRED HERE
mysql_connect("localhost","username","password"); //(host, username, password)
//specify database EDIT REQUIRED HERE
mysql_select_db("database") or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "select * from the_table where 1st_field like \"%$trimmed%\"
order by 1st_field"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
// begin to show results set
echo "Results";
$count = 1 + $s ;
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["1st_field"];
echo "$count.) $title" ;
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
?>
<!-- © http://www.designplace.org/ -->
</body>
</html>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if the codes not suitable for my request ,give me another code to work on it
help me..please