<?php
$login=mysql_connect("localhost","wauhaha_smart",'asd123');
mysql_select_db("wauhaha_smart");


$productname=$_REQUEST['productname'];
$class11=$_REQUEST['className'];
$budget=$_REQUEST['budget'];
$sortby=$_REQUEST['by'];
$sortorder=$_REQUEST['order'];

if($class11!="All"){	// find by class name
//$q=mysql_query("SELECT * FROM product INNER JOIN company ON product.id ='".$_REQUEST['pid']."' AND company.id = product.companyID");


	if(($productname!=null)&&($budget==null)){		//find by product name 
	$q=mysql_query("SELECT * FROM company INNER JOIN product ON pName like '%".$productname."%' AND company.id = product.companyID AND class='".$class11."' ORDER BY $sortby $sortorder ");
	  while($e=mysql_fetch_assoc($q)){
		$output[]=$e;
	}
	
	}
	else if(($productname!=null)&&($budget!=null)){ //find by product name 
	
	$q=mysql_query("SELECT * FROM company INNER JOIN product ON pName like '%".$productname."%' AND company.id = product.companyID AND class='".$class11."' ORDER BY $sortby $sortorder ");
	
	while($display=mysql_fetch_array($q)){
	   
		$price=$display['price'];
	
			if($price<=$budget){
				$output[]=$display;
			
			}
		
				
		}
	}
	else if(($productname==null)&&($budget==null)){ //price and product name is null
	
	$q=mysql_query("SELECT * FROM company INNER JOIN product ON class='".$class11."' AND company.id = product.companyID ORDER BY $sortby $sortorder AND company.id = product.companyID");
	
	while($display=mysql_fetch_array($q)){
	   
		
				$output[]=$display;
			
		}
	}
	
}
else{		//find without class name


	if(($productname==null)&&($budget==null)){		//find by product name 
	$q=mysql_query("SELECT * FROM company INNER JOIN product ON company.id = product.companyID ORDER BY $sortby $sortorder ");
	  while($e=mysql_fetch_assoc($q)){
		$output[]=$e;
	}
	}
	else if(($productname!=null)&&($budget==null)){		//find by product name 
	$q=mysql_query("SELECT * FROM company INNER JOIN product ON pName like '%".$productname."%' AND company.id = product.companyID ORDER BY $sortby $sortorder ");
	  while($e=mysql_fetch_assoc($q)){
		$output[]=$e;
	}
	
	}
	else if(($productname!=null)&&($budget!=null)){ //find by product name 
	$q=mysql_query("SELECT * FROM company INNER JOIN product ON pName like '%".$productname."%' AND company.id = product.companyID ORDER BY $sortby $sortorder ");
	
	while($display=mysql_fetch_array($q)){
	   
		$price=$display['price'];
	
			if($price<=$budget){
				$output[]=$display;
			
			}
		
				
		}
	}
        
		
		
	}

print(json_encode($output));

mysql_close();
?>