Hi,
I have made a search page in which the user enters the keywords to be searched separated with a comma. The code that i ve used works well but it is displaying the name of the user twice.
Eample: There are users registered in the database. Now if
A: C,Perl,Python
B:Java,Perl
For eg: now the user has entered Perl,Python as the keywords.
Then it displays the name A twice and B once.
I wanted that it should display both the name just once.
I have used the following code:
PHP Code:
<?php
$searches = $_SESSION['search'];
//Explode into seperate keywords and make all unique
$terms = array_unique(explode(',',$searches));
//Loop through all terms, and add text for query
foreach($terms as $k=>$v)
{
//Check for empty values
if($v=="") continue;
$terms[$k]="`TagName` LIKE '%$v%'";
}
//Implode each term back together with the OR in between them
$newsearch = implode(' OR ', $terms);
//Append to query string
$query1 = mysql_query("SELECT TagId FROM tagmaster WHERE ".$newsearch."");
while($nt=mysql_fetch_array($query1)){
$query2 = mysql_query("SELECT DISTINCT UserName FROM usertags WHERE TagId = '$nt[TagId]'");
while($nt1=mysql_fetch_array($query2)){
echo "{$nt1[UserName]}";
$result = "SELECT FullName,YearsExp FROM userdetails WHERE UserName = '$nt1[UserName]'";
$data = mysql_query($result) or die(mysql_error());
$resultnum = mysql_num_rows($data);
if($resultnum>0) { // Echos out matches if anything was found
while($info = mysql_fetch_array($data)){
?>
<tr>
<td width="139" valign="top"><p align="center"><a href="searchaccount.php?id=<? echo $info['FullName']; ?>" target=\"_blank\"><? echo $info['FullName']; ?></a></p></td>
<td width="149" valign="top"><p align="center"><? echo $info['YearsExp']; ?></p></td>
</tr>
}
Structure of tables is something like this:
tagmaster
4 C
5 Java
6 Perl
7 Python
usertags
4 A
6 A
7 A
5 B
6 B
userdetails
A Alvin
B Babin
Can you plz tel me what can be possibly wrong in it. I tried using DISTINCT with fullname also but it still dint worked out.