I am creating a web application using php and mySql.
It is basically a simple search form with a single textbox.
The user input can be a combination of keywords, for which I am using php explode() function, after string_ireplace().
Now I want to search each keyword (say val 1, val 2, ..... val n) against each field (say filed 1, filed 2, ...... field n) in a single table.
I feel I will have to use multiple for loops- for each value search all fields.
But how can I sort the result according to relevance, ie. records that match all values will appear first and so on.
Since this table sorting is not at database level, I am not able to use ORDER BY clause.
EDIT: OK. I thought I must explain in detail what I am looking for and what I have achieved. The following code I have written, it almost serves my purpose, but looks quite time consuming(for execution).
<?php
//$str = mysql_real_escape_string($_GET['searchText']);
$str = "val1, val2, val3";
$str = trim($str);
// check for an empty string and display a message.
if ($str == "") {
$resultmsg = "<p>Search Error: Please enter a search keyword...</p>" ;
}
$str = explode(",",$str);
//Create array for all fields
$fields = array("filed1","filed2","filed3","filed4");
$condition = "";
for ($j=0;$j<count($str);$j++){
for ($i=0;$i<count($fields);$i++){
$condition = $condition.$fields[$i]." = '".$str[$j]."' OR ";
}
$condition = rtrim($condition, " OR");
$condition = $condition.") AND (";
}
$condition = rtrim($condition, " AND (");
$sql = "SELECT * FROM TABLE WHERE (".$condition;
echo $sql;
echo "<br /><hr>";
$condition = "";
for ($j=0;$j<count($str);$j++){
for ($i=0;$i<count($fields);$i++){
$condition = $condition.$fields[$i]." = '%".$str[$j]."%' OR ";
}
$condition = rtrim($condition, " OR");
$condition = $condition.") AND (";
}
$condition = rtrim($condition, " AND (");
//$condition = str_replace("="," LIKE ",$condition);
$sql= "SELECT * FROM TABLE WHERE (".$condition;
echo $sql;
echo "<br /><hr>";
//testing
if (count($str)==3){
$condition = "";
for ($j=0;$j<count($str)-1;$j++){
for ($i=0;$i<count($fields);$i++){
$condition = $condition.$fields[$i]." = '%".$str[$j]."%' OR ";
}
$condition = rtrim($condition, " OR");
$condition = $condition.") AND (";
}
$condition = rtrim($condition, " AND (");
//$condition = str_replace("="," LIKE ",$condition);
$sql= "SELECT * FROM TABLE WHERE (".$condition;
echo "<strong>Matching ".$str[0]." AND ".$str[1]."<br /></strong>";
echo $sql;
echo "<br /><hr>";
$condition = "";
for ($j=1;$j<count($str);$j++){
for ($i=0;$i<count($fields);$i++){
$condition = $condition.$fields[$i]." = '%".$str[$j]."%' OR ";
}
$condition = rtrim($condition, " OR");
$condition = $condition.") AND (";
}
$condition = rtrim($condition, " AND (");
//$condition = str_replace("="," LIKE ",$condition);
$sql= "SELECT * FROM TABLE WHERE (".$condition;
echo "<strong>Matching ".$str[1]." AND ".$str[2]."<br /></strong>";
echo $sql;
echo "<br /><hr>";
$condition = "";
for ($j=0;$j<count($str);$j=$j+2){
for ($i=0;$i<count($fields);$i++){
$condition = $condition.$fields[$i]." = '%".$str[$j]."%' OR ";
}
$condition = rtrim($condition, " OR");
$condition = $condition.") AND (";
}
$condition = rtrim($condition, " AND (");
//$condition = str_replace("="," LIKE ",$condition);
$sql= "SELECT * FROM TABLE> WHERE (".$condition;
echo "<strong>Matching ".$str[2]." AND ".$str[0]."<br /></strong>";
echo $sql;
echo "<br /><hr>";
}
?>
The output I am getting is as follows:
Matching all values EXACTLY
SELECT * FROM TABLE WHERE (filed1 = 'val1' OR filed2 = 'val1' OR filed3 = 'val1' OR filed4 = 'val1') AND (filed1 = ' val2' OR filed2 = ' val2' OR filed3 = ' val2' OR filed4 = ' val2') AND (filed1 = ' val3' OR filed2 = ' val3' OR filed3 = ' val3' OR filed4 = ' val3')
Matching all values PARTIALLY
SELECT * FROM TABLE WHERE (filed1 = '%val1%' OR filed2 = '%val1%' OR filed3 = '%val1%' OR filed4 = '%val1%') AND (filed1 = '% val2%' OR filed2 = '% val2%' OR filed3 = '% val2%' OR filed4 = '% val2%') AND (filed1 = '% val3%' OR filed2 = '% val3%' OR filed3 = '% val3%' OR filed4 = '% val3%')
Matching val1 AND val2
SELECT * FROM TABLE WHERE (filed1 = '%val1%' OR filed2 = '%val1%' OR filed3 = '%val1%' OR filed4 = '%val1%') AND (filed1 = '% val2%' OR filed2 = '% val2%' OR filed3 = '% val2%' OR filed4 = '% val2%')
Matching val2 AND val3
SELECT * FROM TABLE WHERE (filed1 = '% val2%' OR filed2 = '% val2%' OR filed3 = '% val2%' OR filed4 = '% val2%') AND (filed1 = '% val3%' OR filed2 = '% val3%' OR filed3 = '% val3%' OR filed4 = '% val3%')
Matching val3 AND val1
SELECT * FROM TABLE WHERE (filed1 = '%val1%' OR filed2 = '%val1%' OR filed3 = '%val1%' OR filed4 = '%val1%') AND (filed1 = '% val3%' OR filed2 = '% val3%' OR filed3 = '% val3%' OR filed4 = '% val3%')
I can now keep on appending the fetched data into my result table. But somehow I dont feel this is a smart solution. Moreover I have a restriction on number of search values(eg. 3 here). I hope I able to explain what exactly I am looking for.