Lenrice Lenrice - 1 month ago 8
PHP Question

Undefined variable in Symfony

Can someone help me with this please I'm getting this error message Notice: Undefined variable: choices, I understand the error but i'm not seeing the issue.

public function getEmployeesArray($conn)
{
// $conn =
// $conn = $this->get('database_connection');
$employees = $conn->fetchall('Select * from vEmployee order by emp_lastname');

foreach ($employees as $emp_row) {
$choices[$emp_row['employee_id']] = $emp_row['emp_lastname'] . ', ' . $emp_row['emp_firstname'];
}

return $choices;
}

Answer Source

Yes $choices undefined before foreach try this :

 public function getEmployeesArray($conn) {
            // $conn = 
    //        $conn = $this->get('database_connection');
            $employees = $conn->fetchall('Select * from vEmployee order by emp_lastname');

            $choices = [];

            foreach ($employees as $emp_row) {

                $choices[$emp_row['employee_id']] = $emp_row['emp_lastname'] . ', ' . $emp_row['emp_firstname'];
            }

            return $choices;
        }