mySQL fields Array ...Remove duplicates

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
big_c147
Forum Newbie
Posts: 15
Joined: Wed Jun 22, 2005 9:32 am

mySQL fields Array ...Remove duplicates

Post by big_c147 »

Hi
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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

array_unique(); Should remove any duplicate values in an array.

http://us2.php.net/manual/en/function.array-unique.php
Post Reply