Results 1 to 10 of 11
Thread: php and mysql search
- 10-11-2004, 04:31 PM #1sdreams Guest
php and mysql search
hello,
my website is mainly regarding vacation rentals of all kind in Spain. All accommodations are added manually and then shown in their respective province ordered alphabetically by town.
I would like to offer a search function to the visitors and hope it is ok to show a link to a page (see information in the center) I prepared in order to explain what I would like to set up:
http://www.spaindreams.com/eng/search.htm
I suppose this search could be done with php and mysql or maybe even with some kind of other program.
My web runs on Linux and PHP and Mysql are installed.
Help on this would be very much appreciated as I have been looking quite a lot for information, but....
Thanks for reading my message
- 10-21-2004, 12:05 PM #2
Guru Wanna Be
- Join Date
- Mar 2004
- Posts
- 836
Well, is your information stored in a MySQL database? In that case you can use some SQL queries to search it. Can you give some more information about your database?
For reliable shared and reseller hosting, visit RadixHosting.
- 10-21-2004, 02:34 PM #3
WHC Guru
- Join Date
- Jan 2004
- Posts
- 3,363
This is one I use. This is basically for static content though.
PHP Code:<?php
/*
* Returns current UNIX timestamp with microseconds.
* This is used to calculate the script execution time.
*
* @return The current time.
*/
function get_microtime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = get_microtime();
/*
* This array contains the URL and path (from this file) to every file to be searched.
* Add every page you want searched.
*
* This works well for a static site were you might have files that
* you don't want searched but for a large site it should probably be
* automatically generated (too hard to maintain otherwise).
*/
$files_to_search = array( "http://www.foo.com/bar.php" => "bar.php",
"http://www.foo.com/bar/bar2.php" => "bar/bar2.php"
);
?>
<html>
<body>
<h1>Search</h1>
<form method="get" action="<?php echo $PHP_SELF; ?>">
<table>
<tbody>
<tr>
<td><strong>Enter Your Search Query:</strong></td>
<td><input type="text" size="20" maxlength="50" name="search_query" value="" /></td>
</tr>
<tr>
<td><strong>Search Type:</strong></td>
<td>
<input type="radio" name="search_type" value="and" <?php if ($search_type == "and"){echo "checked=\"checked\" ";} ?>class="select" />AND
<input type="radio" name="search_type" value="or" <?php if ($search_type != "and"){echo "checked=\"checked\" ";} ?>class="select" />OR</td>
</tr>
<tr align="center">
<td colspan="2"><button type="submit">Search This Site</button></td>
</tr>
</tbody>
</table>
</form>
<?php
/*
* Searches each file for the query terms based on the type of search specified.
*
* @param $files_to_search An array of files to search.
* @param $search_terms An array of terms to search for.
* @param $AND_search A boolean denoting the kind of search to perform.
* @return An array of matching files (URLs) and relevance rankings.
*/
function check_files($files_to_search, $search_terms, $AND_search)
{
//Examine each file in turn.
while (list($url, $file) = each($files_to_search))
{
//Open file for reading and suppress any errors.
@ $fin = fopen($file, "r");
//Report any errors in opening the file.
if (!$fin)
{
echo "<p><strong>WARNING!</strong> Failed to open file (" . $file . ")!</p>";
}
else
{
//Read the contents of the file and strip all tags.
$file_contents = fread( $fin, filesize($file) );
$file_contents = strip_tags($file_contents);
fclose($fin); //Close the file.
//Replace some common HTML entities with their character representations.
$file_contents = str_replace(" ", " ", $file_contents);
$file_contents = str_replace("&", "&", $file_contents);
//Put every word in the file into an array.
$file_contents = explode(" ", $file_contents);
/*
* To ensure that only genuine words are in the array, we must remove non-word chars.
* E.g. "(links)" becomes "links", "links." becomes "links" etc.
*/
$eligible_file_contents = array();
for ($i = 0; $i < count($file_contents); $i++)
{
//If a series of 1 or more word chars are found, add them to the array.
if (preg_match("/(\w+)/i", $file_contents[$i] , $regs))
{
$eligible_file_contents[] = $regs[1];
}
}
$file_contents = $eligible_file_contents;
if ($AND_search)
{
//Needed for relevance ranking.
$term_count = 0;
$num_unique_terms = 0;
//Search for each term in the file.
for ($i = 0; $i < count($search_terms); $i++)
{
$first_occurrence = true;
for ($n = 0; $n < count($file_contents); $n++)
{
//Examine every word in the file with the current search term. case-insensitive.
if (!strcasecmp($file_contents[$n], $search_terms[$i]))
{
$term_count++;
if ($first_occurrence)
{
$num_unique_terms++; //This should only be incremented once for each term.
}
$first_occurrence = false;
}
}
}
//Only store results that found ALL terms (because this is an 'AND' search).
if ($num_unique_terms == count($search_terms))
{
$relevance_ranks["$url"] = get_relevance_rank($term_count, count($file_contents));
}
}
else
{
$term_count = 0; //Tracks the total number of occurrences of the terms (in current file).
//Search for each term in the file.
for ($i = 0; $i < count($search_terms); $i++)
{
for ($n = 0; $n < count($file_contents); $n++)
{
//Examine every word in the file with the current search term. case-insensitive.
if (!strcasecmp($file_contents[$n], $search_terms[$i]))
{
$term_count++;
}
}
}
//Retrieve the relevance of the current document to the query terms.
$relevance_rank = get_relevance_rank($term_count, count($file_contents));
//Only store documents that had matching terms.
if ($relevance_rank > 0)
{
$relevance_ranks["$url"] = $relevance_rank;
}
}
}
}
return $relevance_ranks;
}
/*
* Calculates the relevance of the current document based on the document size
* and the number of times the query terms appeared in it.
*
* @param $num_terms_found The number of times the query terms occurred in the document.
* @param $total_num_words The number of eligible words in the document.
* @return The relevance rank.
*/
function get_relevance_rank($num_terms_found, $total_num_words)
{
/*
* Apply basic normalisation technique and compute relevance score.
*
* Larger files will have a higher number of occurrences of the search terms so we have
* to apply some form of normalisation so the search results aren't skewed in favour of
* larger files. One way to do this is to divide the result for a file by the filesize
* or word count.
*
* Note: To provide an accurate relevance score, we should really apply some form of
* query term weighting (scaling). That is, rare words should have a higher importance
* than common words (stopwords). In addition, we should give priority to terms occurring
* within the title tag or the 'keywords' meta tags. However, for simplicity, I have
* omitted these features.
*/
return round($num_terms_found / $total_num_words, 5);
}
//Determine what kind of search to perform.
if ($search_type == "and")
{
$AND_search = true; //Perform an 'AND' search.
}
else
{
$AND_search = false; //Perform an 'OR' search (default).
}
if($search_query)
{
//Remove any quotes that may have been added (actually replacing '\"').
$search_query = str_replace("\\\"", "", $search_query);
//Split query string into individual search terms and store in an array.
$search_terms = explode(" ", $search_query);
//Find the relevance of each document to the search query. Returns an array (url => relevance).
$file_relevance_ranks = check_files($files_to_search, $search_terms, $AND_search);
//Display details about the search.
echo "<p><strong>Search Query:</strong> " . $search_query . "<br />";
echo "<strong>Search Type:</strong> ";
if ($AND_search)
{
echo "and</p>";
}
else
{
echo "or</p>";
}
echo "<p>" . count($file_relevance_ranks) . " matching document(s) found.</p>";
//Check if any result were returned.
if (count($file_relevance_ranks) == 0)
{
echo "<p>No matching documents were found!</p>";
echo "<p>Suggestions:</p>";
echo "<ul><li>Make sure all words are spelled correctly.</li>";
echo "<li>Try different keywords.</li>";
echo "<li>Try more general keywords.</li></ul>";
}
else
{
//Sort the array with most relevant elements first.
arsort($file_relevance_ranks);
//This will be used to calculate a percentage relevance score.
$highest_relevance = current($file_relevance_ranks);
//Display the search results.
echo "<ul>";
while (list($url, $relevance) = each($file_relevance_ranks))
{
echo "<li>";
//Displays a link to the file and computes a relevance percentage for the file.
echo "<a href=\"$url\">" . $url . "</a> (relevance: " . round(($relevance / $highest_relevance) * 100) . "%)<br />";
echo "</li>";
}
echo "</ul>";
}
//Calculate the total execution time of the script.
$time_end = get_microtime();
$time = $time_end - $time_start;
echo "<p>Searched " . count($files_to_search) . " documents in <strong>" . round($time, 4) . "</strong> seconds.</p>";
}
?>
</body>
</html>
- 10-21-2004, 07:18 PM #4fawh Guest
Fastfind is a good search script and it is written in PHP and uses a MySQL database.
- 12-22-2004, 09:12 AM #5
WHC Guru
- Join Date
- Oct 2004
- Location
- http://kooshin.com
- Posts
- 4,201
there is also phpdig.net which is great .
█ For Reliable and Affordable Web Hosting Packages, Please visit kooshin.com
█ KooshinDesigns.com- Version One Online
█ ArticleStorage.com- Your #1 Resource For Articles
█ 4Ulyrics.com -- The Lyrical Hideout, For Sale - Email me for details.
- 12-22-2004, 09:15 AM #6
WHC Guru
- Join Date
- Jan 2004
- Posts
- 3,363
Use your own
always better
- 12-22-2004, 10:23 AM #7
WHC Guru
- Join Date
- Oct 2004
- Location
- http://kooshin.com
- Posts
- 4,201
it is better right and that is if you know how to make one
(because I don't I guess) or hire someone to a custom one for you
█ For Reliable and Affordable Web Hosting Packages, Please visit kooshin.com
█ KooshinDesigns.com- Version One Online
█ ArticleStorage.com- Your #1 Resource For Articles
█ 4Ulyrics.com -- The Lyrical Hideout, For Sale - Email me for details.
- 12-22-2004, 10:31 AM #8
WHC Guru
- Join Date
- Jan 2004
- Posts
- 3,363
Well its better as you can get 100% of your needs if you do it yourself or hire someone to do it for you
- 02-01-2005, 10:55 AM #9
WHC Guru
- Join Date
- Oct 2004
- Location
- http://kooshin.com
- Posts
- 4,201
The script you posted above you meant it will search any thing that is in public_html and there is no need to search information from the data base ? I will try out
█ For Reliable and Affordable Web Hosting Packages, Please visit kooshin.com
█ KooshinDesigns.com- Version One Online
█ ArticleStorage.com- Your #1 Resource For Articles
█ 4Ulyrics.com -- The Lyrical Hideout, For Sale - Email me for details.
- 02-05-2005, 07:52 AM #10rootsmart.com Guest
if I were you I would put the data into a mysql database, that makes it easier, then just use a Select query statement!



LinkBack URL
About LinkBacks




Reply With Quote






Bookmarks