0

POSTBACK.php

<?php
$q=$_GET['q'];
$words=explode(' ',$q);
//$words2 = "'%".implode("%','%",$words)."%'";
//echo $words2;
$con = mysql_connect('localhost', 'root', '')or  die('Could not connect: ' . mysql_error());

mysql_select_db("ajax_demo", $con)or die('cannot connect');

//$sql = 'SELECT * FROM `ajax_demo_table`  WHERE `FirstName` LIKE CONVERT(_utf8 \'%'.$q.'%\' USING latin1) COLLATE latin1_swedish_ci OR `LastName` LIKE CONVERT(_utf8 \'%'.$q.'%\' USING latin1) COLLATE latin1_swedish_ci'; 
$q = preg_replace('/\s\s+/', ' ', $q); // remove extra space
// $sqlw = "SELECT *,CONCAT(FirstName, ' ',LastName) as fullname FROM `ajax_demo_table` WHERe ";
//$sql = "SELECT *,CONCAT(FirstName, ' ',LastName) as fullname FROM `ajax_demo_table` WHERE CONCAT(FirstName, ' ',LastName) LIKE  CONVERT(_utf8 '%".$words[$i]."%' USING latin1) COLLATE latin1_swedish_ci ";


echo "<table border='1'>";

//if(mysql_fetch_array($result)>=1){
echo"
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
//$found=false;
//$sql = "SELECT *,CONCAT(FirstName, ' ',LastName) as fullname FROM `ajax_demo_table` WHERE CONCAT(FirstName, ' ',LastName) LIKE  CONVERT(_utf8 '%".$words[$wordCount]."%' USING latin1) COLLATE latin1_swedish_ci ";
for($wordCount=0;$wordCount < count($words); ++$wordCount ){
//$sql="SELECT *  FROM `ajax_demo_table` WHERE `FirstName` LIKE CONVERT(_utf8 '%$words[$wordCount]%' USING latin1) COLLATE latin1_swedish_ci OR `LastName` LIKE CONVERT(_utf8 '%$words[$wordCount]%' USING latin1) COLLATE latin1_swedish_ci";
$sql = "SELECT *,CONCAT(FirstName, ' ',LastName) as fullname FROM `ajax_demo_table` WHERE CONCAT(FirstName, ' ',LastName) LIKE  CONVERT(_utf8 '%".$words[$wordCount]."%' USING latin1) COLLATE latin1_swedish_ci ";

//echo $wordElement=$words[$i];
$result = mysql_query($sql) or die('cannot query '.mysql_error($result));
$found=false;
//$rowTest=mysql_fetch_array($result);



while($row = mysql_fetch_array($result))
  {
 $found=true;
  echo "<tr>";
  echo "<td bgcolor='#ffff80'>" . $row['FirstName'] . "</td>";
  echo "<td bgcolor='#aaff50'>" . $row['LastName'] . "</td>";
  echo "<td bgcolor='#80ffff'>" . $row['Age'] . "</td>";
  echo "<td bgcolor='#ff8c8c'>" . $row['Hometown'] . "</td>";
  echo "<td  bgcolor='#ff8040'>" . $row['Job'] . "</td>";
  echo "</tr>";
  }}
if(!$found){
	  //for($wordCount2=0;$wordCount2 < count($words); ++$wordCount2 ){
echo "<tr align='center'><td colspan=5 bgcolor='yellow'><b>NO<b> similar names with <font color=red><b>"./*$words[$wordCount2]*/$q.'</b></font></td></tr>';
  }//}



/*else{
echo "<tr><td><b>NO<b> similar names with <font color=red><b>".$words[$i].'</b></font></td></tr>';

}*/

//}

//end of while
//}

//}
echo "</table>";
mysql_close($con);
?>

What the client user wants now is to eliminate duplicating values. One thread I posted yesterday was solved but I repost it because of another problem... I revised the code for the table format but If there's someone out there who could help me in eliminating duplicate values

the full code was herehttp://www.daniweb.com/web-development/php/threads/367519

again please help me how to eliminate duplicating values... it goes like this way... I have sample peter griffin wherein stored in an array [peter] [griffin] and so on because of the explode function. I want now to know how to eliminate duplicating values because when I put Peter Griffin... the Peter Griffin is duplicated to show up

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by masterjiraya
1

Here you can see I have executed query only once, and shortened for loop.

$operator="";
$condition="";
for( $i = 0; $i < count($words); ++$i ){
	$condition.=$operator." CONCAT(FirstName, ' ',LastName) LIKE  CONVERT(_utf8 '%".$words[$i]."%' USING latin1) COLLATE latin1_swedish_ci";
	$operator=" OR ";
}
$sql = "SELECT *,CONCAT(FirstName, ' ',LastName) as fullname FROM `ajax_demo_table`   ";

if ($condition!="")
$sql .= " where ($condition) ";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.