Add Filter Using ExecuteRows on Recordset_Selecting

Good day I want to add Filter for specific record based on fetching array emp_id but my code doesnt seem to work im just new to phpmaker , please help thank you.

$unit=CurrentUserInfo("unit");
if ($userlevel == 1) {
         $empid = ExecuteRows("SELECT emp_id FROM simc_users WHERE unit='$unit'");
        $empids=['$empid'];
        foreach ($empids as $emp_id) {
            AddFilter($filter, "emp_id = '$emp_id'");
        }
        
    };

JAYM wrote:

if ($userlevel == 1) { // ← Where is $userlevel from?
$empid = ExecuteRows(“SELECT emp_id FROM simc_users WHERE unit=‘$unit’”); // ← ExecuteRows() returns an array of records
$empids=[‘$empid’]; // ← Wrong syntax
foreach ($empids as $emp_id) {
AddFilter($filter, “emp_id = ‘$emp_id’”); // ← This adds filter by “AND”, not “OR”
}
}

>

I updated the code

$unit=CurrentUserInfo("unit");

if ($userlevel == 1) { // Assuming $userlevel is defined elsewhere

    $empids = ExecuteRows("SELECT emp_id FROM simc_users WHERE unit='$unit'"); // Assuming ExecuteRows() returns an array of records

    $filterConditions = []; // Initialize an array to store filter conditions

    foreach ($empids as $emp_id) {
        $filterConditions[] = "emp_id = '$emp_id[emp_id]'"; // Assuming 'emp_id' is the column name in the database
    }

    // Join filter conditions with 'OR'
    $filter = implode(' OR ', $filterConditions);


        AddFilter('emp_id', $filterConditions);
    
};

but I get this error
D:\xampp\htdocs\bio\models\SimcOffduty.php(1432): PHPMaker2024\AMS\AddFilter(): Argument #1 ($filter) cannot be passed by reference

You should keep using $filter as the first param of AddFilter global function.Change this code:

AddFilter('emp_id', $filterConditions);

to:

AddFilter($filter, $filterConditions);

Already updated the code but no error but no records found

if ($userlevel == 1) { // Assuming $userlevel is defined elsewhere

    $empids = ExecuteRows("SELECT emp_id FROM simc_users WHERE unit='$unit'"); // Assuming ExecuteRows() returns an array of records

    $filterConditions = []; // Initialize an array to store filter conditions

    foreach ($empids as $emp_id) {
        $filterConditions[] = "emp_id = '$emp_id[emp_id]'"; // Assuming 'emp_id' is the column name in the database
    }

    // Join filter conditions with 'OR'
    $filter = implode(' OR ', $filterConditions);


    // Assuming AddFilter function accepts at least two arguments (fieldName and filterValue)
    // Modify this according to your actual implementation in PHPMaker2024\AMS namespace
    foreach ($filterConditions as $filterCondition) {
        AddFilter($filter, $filterCondition);
    }
};

You better try to double-check the output of $filterCondition variable, to make sure your code is correct.

JAYM wrote:

$filterConditions = “emp_id = ‘$emp_id[emp_id]’”; >

Wrong syntax, it should be $emp_id[“emp_id”]. It might be simpler to use “.” to concatenate instead of putting variable in double quoted string here.

Finally fixed the code guys thank you for your help
this is the final code finally able to select data based on the unit of users and get the ids as filter condition

$unit=CurrentUserInfo("unit");

 if ($userlevel == 1) { // Assuming $userlevel is defined elsewhere

    $empids = ExecuteRows("SELECT emp_id FROM simc_users WHERE unit='$unit'"); // Assuming ExecuteRows() returns an array of records

    $filterConditions = []; // Initialize an array to store filter conditions

    foreach ($empids as $emp_id) {
        $filterConditions[] = "emp_id = '" . $emp_id["emp_id"] . "'"; // Assuming 'emp_id' is the column name in the database
    }

    // Join filter conditions with 'OR'
    $filters = implode(' OR ', $filterConditions);

    foreach ($filterConditions as $filterCondition) {
        AddFilter($filter, $filters);
    }
};

JAYM wrote:

// Join filter conditions with 'OR'
$filters = implode(' OR ', $filterConditions);
foreach ($filterConditions as $filterCondition) {
    AddFilter($filter, $filters);
}
>

Review above code.