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:
[code=php]
<?php
$searches = $_SESSION['search'];
//Explode into seperate keywords and make all unique
$terms = array_unique(ex plode(',',$sear ches));
//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("SE LECT TagId FROM tagmaster WHERE ".$newsearch."" );
while($nt=mysql _fetch_array($q uery1)){
$query2 = mysql_query("SE LECT DISTINCT UserName FROM usertags WHERE TagId = '$nt[TagId]'");
while($nt1=mysq l_fetch_array($ query2)){
echo "{$nt1[UserName]}";
$result = "SELECT FullName,YearsE xp FROM userdetails WHERE UserName = '$nt1[UserName]'";
$data = mysql_query($re sult) or die(mysql_error ());
$resultnum = mysql_num_rows( $data);
if($resultnum>0 ) { // Echos out matches if anything was found
while($info = mysql_fetch_arr ay($data)){
?>
<tr>
<td width="139" valign="top"><p align="center"> <a href="searchacc ount.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>
}
[/code]
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.
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:
[code=php]
<?php
$searches = $_SESSION['search'];
//Explode into seperate keywords and make all unique
$terms = array_unique(ex plode(',',$sear ches));
//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("SE LECT TagId FROM tagmaster WHERE ".$newsearch."" );
while($nt=mysql _fetch_array($q uery1)){
$query2 = mysql_query("SE LECT DISTINCT UserName FROM usertags WHERE TagId = '$nt[TagId]'");
while($nt1=mysq l_fetch_array($ query2)){
echo "{$nt1[UserName]}";
$result = "SELECT FullName,YearsE xp FROM userdetails WHERE UserName = '$nt1[UserName]'";
$data = mysql_query($re sult) or die(mysql_error ());
$resultnum = mysql_num_rows( $data);
if($resultnum>0 ) { // Echos out matches if anything was found
while($info = mysql_fetch_arr ay($data)){
?>
<tr>
<td width="139" valign="top"><p align="center"> <a href="searchacc ount.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>
}
[/code]
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.
Comment