Home > Enterprise >  Filtering data only if match with all get elements
Filtering data only if match with all get elements

Time:12-29

I want to make a filter by comparing the elements of my table to the parameters that I pass in $_GET

if(isset($_GET['skills']) && !empty($_GET['skills']))
            {
                $memberModel->where("t1.id IN(SELECT TMS.member_id FROM `".memberSkillModel::factory()->getTable()."` AS `TMS` WHERE `TMS`.skill_id IN(".join(",", $_GET['skills'])."))");
            }

with this query, I display result sets if there is a match with any of the given values. Can I display result sets only if there is a match with all of the given values but no with any of them ?

thank you

CodePudding user response:

Assuming that's no duplicate entry? Could you try some think like this

if (isset($_GET['skills']) && 
    is_array($_GET['skills']) &&
    !empty($_GET['skills'])) {
    
    $skills = $_GET['skills'];
    //sanitiz
    $skills = array_map(function($i) { return intval($i); }, $skills);
    $size = count($skills);
    
    $sql = sprintf("t1.id IN (
select 
    TMS.member_id, count(*) as c  
from
    `%s` as `TMS` 
where 
    `TMS`.skill_id in (%s)
group by 
    TMS.member_id
having 
    count(*) = %d
    )", 
    $memberSkillModel::factory()->getTable(),
    join(",", $skills),
    $size);
    
    $memberModel->where($sql);
}

CodePudding user response:

array_walk_recursive(array_filter($_GET), fucntion($term) {

WHERE $term IN ()

});
  • Related