CakePHP - populating select form

后端 未结 2 753
Happy的楠姐
Happy的楠姐 2021-02-11 11:03

I\'m trying to populate a drop down select form with values from a database.

Here is what I have currently.

$modes = Set::combine($this->Setting->         


        
2条回答
  •  無奈伤痛
    2021-02-11 11:20

    You want to loop through modes and create an option for each mode, like so:

    $options = array();
    
    foreach($modes as $mode)
    {
        $options[$mode] = "Title " . $mode;
    }
    
    echo $form->select('current_mode', $options);
    

    You can either put the above logic in your view, or you can do it in your controller, and then set the variable like this:

    $this->set("options", $options);
    

    The docs here explain the select element method pretty well:

    http://book.cakephp.org/view/1430/select

提交回复
热议问题