0

I'm trying to eliminate some items in an array by checking some other profile information in the database matching the user id's.

function filterSexMatches($sSex, $aMatchIds){
    foreach($aMatchIds as $iMatchId){
        $aMatches[] = $this->getOne("SELECT `ID` FROM `Profiles` WHERE `ID` = '{$iMatchId}' AND `Sex` != '{$sSex}'");           
    }
    return $aMatches;
}

This is my function but it returns an empty id value when the Sex doesn't match and adds it to the $aMatches array.

I want to know how I can change this to skip over the user id if the Sex field matches so it doesn't return an empty iteration.

0

2 Answers 2

1
function filterSexMatches($sSex, $aMatchIds){
    $aMatches = array();
    foreach($aMatchIds as $iMatchId){
        $data = $this->getOne("SELECT `ID` FROM `Profiles` WHERE `ID` = '{$iMatchId}' AND `Sex` != '{$sSex}'");           
        if (!empty($data)) {
            $aMatches[] = $data;
        }
    }
    return $aMatches;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Personally I would use a single query to get all profiles and then iterate over the result in php, matching and adding to an empty array. Instead of executing a single query for every id.

Not exactly sure how you're system works but I'll try to give ex.

$result = $this->getOne("SELECT * FROM Profiles WHERE ... ");

$newArray = array();
foreach($result as $row) {
    if($row->sex == $sSex) {
        $newArray[] = $row;
    }
}
return $newArray;

1 Comment

Perhaps this would be better off a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.