This code is a part of a very detailed seach using pagination with two seperate tables. I have tried using implode() explode() for the groupings but couldn't get the pagination to work with it. This code works perfectly as is, but I feel I could do better to speed things up. Thanks for any advice!
<?
$query = "SELECT distinct a.* FROM profile as a, images as b WHERE ";
////// first grouping ///////
$query .= " (a.age between '$age1' and '$age2')";
////// second grouping - these grouping are set up so that if a user doesn't check off a selection in the search form the query will produce all results from that particular group. If a selection is made, only the checked selection will be searched for. /////////
if(!empty($sex1) or !empty($sex2) or !empty($sex3)or !empty($sex4))
$query .= " AND(a.sex in('$sex1 ','$sex2 ','$sex3 ','$sex4 '))";
////// third grouping //////////
if(!empty($ethnicity1) or !empty($ethnicity2) or !empty($ethnicity3) or !empty($ethnicity4) or !empty($ethnicity5) or !empty($ethnicity6) or !empty($ethnicity7) or !empty($ethnicity8) or !empty($ethnicity9))
$query .= " AND (a.ethnicity in ('$ethnicity1 ','$ethnicity2 ','$ethnicity3 ','$ethnicity4 ',
'$ethnicity5 ','$ethnicity6 ','$ethnicity7 ','$ethnicity8','$ethnicity9'))";
////// fourth grouping/////////
if(!empty($bodytype1) or !empty($bodytype2) or !empty($bodytype3) or !empty($bodytype4) or !empty($bodytype5) or !empty($bodytype6) or !empty($bodytype7))
$query .= " AND (a.bodytype in ('$bodytype1 ','$bodytype2 ','$bodytype3 ','$bodytype4 ', '$bodytype5 ','$bodytype6 ','$bodytype7 '))";
////// there are about five more groupings after this but I think you get the idea here///
if(!empty($photo))
$query .=" AND a.userid = b.userid AND ( b.img_id != 0)";
$query .= " order by last_login desc";
?>
This code does work perfectly and I have no problems with it. I do feel however that there must be a more efficient way. Thanks for any help!