0

I'm trying to return the content of a loop in PHP (for a select) Here is my code :

public function select($name, $field, $table) {
    $query = $this->db->query("SELECT $field FROM $table", []);
    $datas = $query->fetchAll();
    $number = $query->rowCount();
    return '<select class="form-control" name="' . $name . '">
                <option value="">' . $name . '</option>'
                    for ($i=0; $i < $number; $i++) { 
                        '<option value="' . $datas[$i]->$field . '" >' . $datas[$i]->$field . '</option>'
                    }
            '</select>';
}

I get an error : ( ! ) Parse error: syntax error, unexpected 'for' (T_FOR)

I know my code is wrong, but I can't figure an easy solution to accomplish what I want.

Thanks for your help !

Cephou
  • 257
  • 5
  • 23

1 Answers1

2

Do something like that:

$string = '';
$string .= '<select>';
$string .= '<option value="">xxx</option>';
for($i=0; $i < $number; $i++) {
    $string .= '<option value=""></option>';
}
$string .= '</select>';
return $string;
Tomasz Winter
  • 287
  • 1
  • 9