K
K
Konstantin782014-10-08 17:24:04
Kohana
Konstantin78, 2014-10-08 17:24:04

How to correctly form the parameter for "Form::select"?

Good day to all.
I work with kohana 3.3.2
There is a table of departments (id., name of the department, subordinate to the department), with the fields "id", "name", "main_dep_id".
The "main_dep_id" field refers to the "name" field of another "id" of the same table (i.e. a department is part of another department, etc.).
In the department edit controller handler, I wrote:

...
         $id = (int) $this->request->param('id');
         $one_department_a = ORM::factory('department', $id)->as_array();
         $this->departments_a = array();
         $this->departments_a1 = array();
        foreach($this->departments_o as $department)
        {
            $this->departments_a[$department->id] = $department->abbreviation;
            $this->departments_a1[$department->id]= $department->name;
        }
...

In the view of editing departments, the parameter (field) "is subordinate to the department" I made using "select" (with the choice of already existing departments)
…
      <select size="1" name="main_dep_id">
           <?php foreach($departments as $department):?>
                 <?php if(($department->id) == $one_department_a['main_dep_id']):?>
                     <option selected value="<?=($department->id)?>"><?=$department->name?></option>
                 <?php else:?>
                     <option value="<?=($department->id)?>"><?=$department->name?></option>
                 <?php endif?>    
           <?php endforeach?>   
       </select>
…

Everything works (maybe the code is not quite well written).
But here (in the view), I wanted to use the kohana method, which has "Form::select"
( public static function select($name, array $options = NULL, $selected = NULL, array $attributes = NULL) ),
but I can’t figure out how to form the third parameter of this method? Is it somehow formed through the model?
Or is it better to leave everything as it is?
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tesla, 2014-10-17
@Tesla

Probably not the third parameter but the second one? It should look like this:

$options = array(
  'value1' => 'name1',
  'value2' => 'name2',
);

If optgroups are needed, then:
$options = array(
  'group1' => array(
    'value1' => 'name1',
    'value2' => 'name2',
  ),
  'group2' => array(
    'value3' => 'name3',
  ),
);

And as the third parameter, simply the value value is accepted.
$selected = 'value1';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question