I have made a page which performs a search on a mySQL database based on a form. The problem i have is that the same record is shown more than once it it meets once or more of the criteria.
for example.....
if the title of a record in the database is "This is a Test"
and i search for "is Test"
the record will be shown three times (because "is" is contained the word "this" and "is" and it also matched the second word "Test" thus making three hits.
I think the solution i need is to put all fields pulled from the database into a array and then remove the duplicates however i am unsure how to do this... can anyone help?
here is the relevant code i am using
Code: Select all
$sm= $_POST['StartMonth'];//get the variables posted from the form
$sy= $_POST['StartYear'];
$em = $_POST['EndMonth'];
$ey = $_POST['EndYear'];
$key = $_POST['Key'];
$ConEnd = strtotime ("1 $em $ey");//convert dates from text to time()
$month = date(m,$ConEnd);//work out which month it is
$days = cal_days_in_month(CAL_GREGORIAN, $month, $ey);//work out how many days in that month
$ConStart = strtotime ("1 $sm $sy");//convert to time()
$ConEnd = strtotime ("$days $em $ey");//convert to time()
//trim whitespace from the stored variable
$trimmed = trim($key);
//separate key-phrases into keywords
$trimmed_array = explode(" ",$trimmed);
foreach ($trimmed_array as $trimm)//for all the words entered
{//start for-next
$sql= "SELECT DISTINCT * FROM tblnotes WHERE CompanyName LIKE'$com'
AND Date >=$ConStart AND Date <= $ConEnd AND Title LIKE '%$trimm%'
ORDER by DATE DEsC";
//select all records matching the key words,dates and company name
$result = mysql_query($sql,$odbc) or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) //while there are records
{
$date = $row[Date];
$realdate = date('l dS M Y G:i:s', $date);//convert date to string
echo"<TR>";//put the relevant rows into a table
echo"<TD>$row[Title]</TD>";
echo"<TD> $realdate </TD>";
echo"<TD> $row[CompanyName] </TD>";
echo"<TD>$row[Username]</TD>";
echo"</TR>";
}
}//exit for-next