HI All,

my requirement is as below:
lets product name is "aaa bbb ccc".
Now in search coding i have use LIKE statement.

"... where product_name LIKE '%".$keyword."%'"

Now when user insert 'aaa' or 'bbb' or 'ccc' or 'aaa bbb' or 'bbb ccc' search works.
But if user insert 'aaa ccc' then it is not working.
I want such search result where 'aaa ccc' will also comes in result.

Help me !!

Just a quick thought: you can explode your search term in to words and check with each of them.

I think it wont work,

e.g product name "blackberry bold 2200",
and i would search with "blackberry 2200"
then if i explode then all "blackberry" would comes.. i just want blackberry 2200.

This is also what google do.
But i don't know how they checks?

I am not a professional person... But shouldn't it be working?

"... where (product_name LIKE '%".$keyword[1]."%') AND (product_name LIKE '%".$keyword[2]."%')..."

Hope somebody will help you. I am interested in this problem too :]

<?php 		

$_POST['keywords']="aaa    ccc";

for($i=0;$i<10;$i++)
	$_POST['keywords']=str_replace("  "," ",$_POST['keywords']);

$arrkey=explode(" ",trim($_POST['keywords']));	


$query="select * from product where somecol='somevalue' ";

$keycount=count($arrkey);
if($keycount>0 and trim($_POST['keywords'])!="")
{
	$query.=" and (";
	$operator="";
	for($i=0;$i<$keycount;$i++)
	{
		$query.=$operator."(product_name like '%{$arrkey[$i]}%')";
		$operator=" or ";		
	}
	$query.=")";
}
echo $query;

?>
<?php 		

$_POST['keywords']="aaa    ccc";

for($i=0;$i<10;$i++)
	$_POST['keywords']=str_replace("  "," ",$_POST['keywords']);

$arrkey=explode(" ",trim($_POST['keywords']));	


$query="select * from product where somecol='somevalue' ";

$keycount=count($arrkey);
if($keycount>0 and trim($_POST['keywords'])!="")
{
	$query.=" and (";
	$operator="";
	for($i=0;$i<$keycount;$i++)
	{
		$query.=$operator."(product_name like '%{$arrkey[$i]}%')";
		$operator=" or ";		
	}
	$query.=")";
}
echo $query;

?>

This code will generate below query:

$query="select * from product where somecol='somevalue' and ( (product_name like '%aaa%') or (product_name like '%ccc%') ) ";

But again, this will popup all record which have 'aaa' but not 'ccc'.

I am not a professional person... But shouldn't it be working?

"... where (product_name LIKE '%".$keyword[1]."%') AND (product_name LIKE '%".$keyword[2]."%')..."

Hope somebody will help you. I am interested in this problem too :]

This is simple logic and it should work.. let me check.

CHANGE MY LINE NO 21 OF PREVIOUS POST LIKE GIVEN BeLOW, remove or then write AND

$operator=" and ";

Thanks urtrivedi and Daiva.
The logic of both is same and it is so simple.
I dont know why it was not sparked for me even its so simple, anyways it happens sometimes :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.