$host="localhost";
$un="username";
$pw="password";
$db_name="databasename";
$error_query_not_entered="Вы ничего не ввели в окно поиска. Пожалуйста,
повторите поиск";
if ($q=="")
{
echo $error_query_not_entered;
}
else
{
$q_array=explode(" ", $q);
$q_num=(integer)count($q_array);
echo "Вы искали <b>".$q."</b>";
}
$dbh=mysql_connect ($host, $un, $pw) or die ('I cannot connect to the
database.');
mysql_select_db ($db_name);
$r=query_table("GMAT", $q_num, $q_array);
print_answers ("GMAT Vocabulary",
"http://www.obrazovanie.info/gmat/gmat.php?page=",
$r);
$r=query_table("SAT", $q_num, $q_array);
print_answers ("SAT Vocabulary",
"http://www.obrazovanie.info/sat/sat.php?page=",
$r);
$r=query_table("TOEFL", $q_num, $q_array);
print_answers ("TOEFL Vocabulary",
"http://www.obrazovanie.info/toefl/toefl.php?page=",
$r);
$r=query_table("GRE", $q_num, $q_array);
print_answers ("GRE Vocabulary",
"http://www.obrazovanie.info/gre/gre.php?page=",
$r);
$r=query_html ("$DOCUMENT_ROOT/articles/",
$q_num, $q_array);
print_answers ("Статьи",
"http://www.obrazovanie.info/articles/",
$r);
function query_table ($tablename, $q_num, $q_array)
{
$query="SELECT page, contents from $tablename";
$result=mysql_query($query);
$num_results=mysql_num_rows($result);
for ($c=0;$c<$num_results;$c++)
{
$relevancy=0;
$row=mysql_fetch_object($result);
$mypage=$row->number;
$mycontent=strtolower(strip_tags($row->article));
$d=0; $d<$q_num; $d++)
{
$relevancy+=substr_count($mycontent,
strtolower(strip_tags($q_array[$d])));
}//end of for
if ($relevancy>0)
$res["$mypage"]=$relevancy;
}//end of for
if (count($res)>0)
arsort ($res);
return $res;
}//end of query_table
function query_html ($dir, $q_num, $q_array)
{
//chdir($dir);
$current_dir=opendir($dir);
while ($current_file=readdir($current_dir))
{
if ($current_file=="." || $current_file=="..")
continue;
$fn="$dir"."$current_file";
$filearray = file("$fn");
$relevancy=0;
$mypage="$current_file";
$mycontent=strtolower(strip_tags(implode (" ", $filearray)));
for ($d=0; $d<$q_num; $d++)
{
$relevancy+=substr_count((string)$mycontent,
(string)strtolower(strip_tags($q_array[$d])));
}//end of for
if ($relevancy>0)
$res["$mypage"]=$relevancy;
}//end of while
if (count($res)>0)
arsort ($res);
return $res;
}//end of query_html
function print_answers ($zonename, $zoneurl, $r)
{
$c=0;
echo $zonename;
if (count($r)<1)
echo "Ничего не нашел<br>";
else
{
while ($element=each($r))
{
if ($c<5)
{
echo "<li>";
echo "<a href=";
echo $zoneurl.$element["key"];
echo ">";
echo "Материал номер ".$element["key"]."</a>";
echo "</li>\n";
$c++;
}
}//end of while
}//end of else
}//end of print_answers
|