-1

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;
}
DonCallisto
  • 29,419
  • 9
  • 72
  • 100
Lenrice
  • 13
  • 2
  • 9
  • `... $choices = array(); foreach ($employees as $emp_row) { ....` – Max P. Aug 22 '17 at 10:48
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – alpakyol Aug 22 '17 at 11:34

2 Answers2

3

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;
        }
2

try this:

public function getEmployeesArray($conn) {   
    $choices = []; //or what you want

    $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;
}

You need to initialize $choices because if there isn't $employees It never been set.

The problem now is your query that doesn't return any value so your code doesn't enter inside your foreach loop

Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171