Jump to content

Add Weights to site search script (php_mysql)


disconne

Recommended Posts

All helps will be appreciated. I am only a succesfull code copier unfortunately & a little bit modifier.

After explainig my requirement, I pasted my site search php codes.

Best Regards

 

MySql Table

id author title text

 

My weights

if searchterm(s) found in column AUTHOR : 100

if searchterm(s) found in column TITLE : 91

if searchterm(s) found in column TEXT : 15

 

I want that code calculates the # of returns for each row columns. then multiply with coefficients above and make the ranking from highest points to lowest.

 

An example: Assume that search terms are: "php web John"

in my table row 1 -> 1 result for Author -> so total points: 1 * 100 = 100

in my table row 2 -> 1 result for Title -> so total points: 1 * 91 = 91

in my table row 3 -> 6 results for Text -> so total points: 6 * 15 = 90

in my table row 4 -> 1 result for Author & 1 result for Title -> so total points: (1 * 100)+(1 * 91) = 191

 

so ranking for display should be

row 4

row 1

row 2

row 3

 

<?php

//Get variables from config.php to connect to mysql server
require 'config.php';
require 'opendb.php';
mysql_query("SET NAMES 'latin5'");
mysql_query("SET CHARACTER SET latin5");
mysql_query("SET COLLATION_CONNECTION = 'latin5_turkish_ci'");

//search variable = data in search box or url
if(isset($_GET['search']))
{
$search = $_GET['search'];
}

//trim whitespace from variable
$search = trim($search);
$search = preg_replace('/\s+/', ' ', $search);

//seperate multiple keywords into array space delimited
$keywords = explode(" ", $search);

//Clean empty arrays so they don't get every row as result
$keywords = array_diff($keywords, array(""));

//Set the MySQL query
if ($search == NULL or $search == '%'){
} else {
for ($i=0; $i<count($keywords); $i++) {
$query = "SELECT * FROM dis_yazilar " .
"WHERE title LIKE '%".$keywords[$i]."%'".
" OR author LIKE '%".$keywords[$i]."%'" .
" OR text LIKE '%".$keywords[$i]."%'" .
" ORDER BY title";
}

//Store the results in a variable or die if query fails
$result = mysql_query($query) or die(mysql_error());
}
if ($search == NULL or $search == '%'){
} else {
//Count the rows retrived
$count = mysql_num_rows($result);
}

echo "<html>";
echo "<head>";

echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-9\">";

echo "<title>Your Title Here</title>";
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"../style.css\" />";
echo "</head>";
echo "<body onLoad=\"self.focus();document.searchform.search.focus()\">";
echo "<center>";
echo "<br /><form name=\"searchform\" method=\"GET\" action=\"ara.php\">";
echo "<input type=\"text\" name=\"search\" size=\"20\" TABINDEX=\"1\" />";
echo " <input type=\"submit\" value=\"Search\" />";
echo "</form>";
//If search variable is null do nothing, else print it.
if ($search == NULL) {
} else {
echo "You searched for <b><FONT COLOR=\"blue\">";
foreach($keywords as $value) {
   print "$value ";
}
echo " # of search results </font></b>" . $count;
}
echo "<p> </p><br />";
echo "</center>";

//If users doesn't enter anything into search box tell them to.
if ($search == NULL){
echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>";
} elseif ($search == '%'){
echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>";
//If no results are returned print it
} elseif ($count <= 0){
echo "<center><b><FONT COLOR=\"red\">Your query returned no results from the database.</font></b><br /></center>";
//ELSE print the data in a table
} else {
//Table header
echo "<center><table id=\"search\" bgcolor=\"#AAAAAA\">";
echo "<tr>";
echo "<td><b>title:</b></td>";
echo "<td><b>author:</b></td>";
echo "<tr>";
echo "</table></center>";

//Colors for alternation of row color on results table
$color1 = "#d5d5d5";
$color2 = "#e5e5e5";
//While there are rows, print it.
while($row = mysql_fetch_array($result))
{
//Row color alternates for each row
$row_color = ($row_count % 2) ? $color1 : $color2;
//table background color = row_color variable
echo "<center><table bgcolor=".$row_color.">";
echo "<tr>";
echo "<td>".$row['title']."</td>";
echo "<td>".$row['author']."</td>";
echo "</tr>";
echo "</table></center>";
$row_count++;
//end while
}
//end if
}

echo "</body>";
echo "</html>";
if ($search == NULL or $search == '%') {
} else {
//clear memory
mysql_free_result($result);
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.