开发者

PHP/MySQL relational database problem. Filter to match options

I have a table of people who need help, and a table of people who give help. They each have a one to many relationship with further tables for work hours, hobbies and activities.

My problem is that when I am creating a filter in order to match them up I cant figure out the correct collection of queries to match them fully.

function buildfilter(){
    if($_GET['filter']=="clear" || $_GET['filt_age']<>""){$_SESSION['filter']="";}
    if($_SESSION['filter']==""){$query="SELECT `personal_assistant`.*,`hobby`.* FROM `personal_assistant`,`hobby`,`citizen`,`citizenhobby` WHERE `personal_assistant`.`status`='active'";}
    if($_GET['filt_car']=="1"){$query.=" AND `personal_assistant`.`licence`='1' AND `personal_assistant`.`car`='1'";}
    if($_GET['filt_age']<>"any" AND $_GET['filt_age']<>""){
        $today=date ( 'Y-m-j' , time());
        $ages = explode("-", $_GET['filt_age']);
        $ages[0]=strtotime ('-'.$ages[0].' year', strtotime($today));
        $ages[1]=strtotime ('-'.$ages[1].' year', strtotime($today));
        $query.=" AND `personal_assistant`.`dob`>='".$ages[1]."' AND `personal_assistant`.`dob`<='".$ages[0]."'";
    }
    if($query<>""){
        $query.=" AND (`personal_assistant`.`id`=`hobby`.`pa_id` AND `hobby`.`hobby_option_id`=`citizenhobby`.`hobby_option_id` AND `citizen`.`id`=`citizenhobby`.`ci_id` AND `citizen`.`id`='".$_GET['edcit']."')";
        $query.=" AND (`personal_assistant`.`id`=`activity`.`pa_id` AND `activity`.`activity_option_id`=`citizenactivity`.`activity_option_id` AND `citizen`.`id`=`citizenactivity`.`ci_id` AND `citizen`.`id`='".$_GET

['edcit']."')";
    }
    if($query==""){$query=$_SESSION['filter'];}
    if($_SESSION['filter']==""){$_SESSION['filter']=$query;}

    return $query;
}

That function so far would correctly return any personal assistants that have atleast one hobby that matches and atleast one activity that matches. What I cannot do however is make it only return people who match all of the activities instead of just one as these are all required.

The query string looks like this so far:

SELECT 
    `personal_assistant`.*,
    `hobby`.* 
FROM 
    `personal_assistant`,
    `hobby`,
    `citizen`,
    `citizenhobby` 
WHERE 
 开发者_开发技巧   `personal_assistant`.`status`='active' 
AND (
        `personal_assistant`.`id`=`hobby`.`pa_id` 
        AND 
        `hobby`.`hobby_option_id`=`citizenhobby`.`hobby_option_id` 
        AND 
        `citizen`.`id`=`citizenhobby`.`ci_id` 
        AND 
        `citizen`.`id`='6'
    ) 


multiple queries would achieve it

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜