简体   繁体   English

CakePHP:如何从数据库创建OptGroup下拉列表?

[英]CakePHP : How to Create OptGroup Drop down list from DataBase?

i am new to CakePHP, and developing a new project. 我是CakePHP的新手,正在开发一个新项目。 i just want to know that, how to show optgroup dropdown list that is fetched from database. 我只想知道,如何显示从数据库中获取的optgroup下拉列表。

i want to fetch array like this below 我想在下面获取这样的数组

$arr = array(
    'optgroup' => array(
        '1','2','3'),
     'optgroup2' => array(
         '1','2','3')
);     

I'm assuming you already have your model set up. 我假设您已经建立了模型。

In your controller you will need to assign variables to the results of find(list) calls and then set() them. 在您的控制器中,您需要为find(list)调用的结果分配变量,然后对它们进行set() Here's an example from a project where I have dropdowns for requests, currencies, etc.: 这是一个项目的示例,其中有请求,货币等的下拉列表:

    $requests = $this->Purchase->Request->find('list');
    $currencies = $this->Purchase->Currency->find('list', $options);
    $shippingCurrencies = $this->Purchase->ShippingCurrency->find('list', $options);
    $finalCurrencies = $this->Purchase->FinalCurrency->find('list', $options);
    $receivers = $this->Purchase->ReceiverUser->find('list');
    $this->set(compact('requests', 'currencies', 'shippingCurrencies', 'finalCurrencies', 'receivers'));

(You can also do that with a series of $this->set(...) calls rather than using variables and the $this->set(compact(...)) .) (您也可以通过一系列$this->set(...)调用来实现,而不是使用变量和$this->set(compact(...)) 。)

Then in your view you use the Form->input() call to make the select statement. 然后在您的视图中,使用Form->input()调用来执行select语句。 Here's another example: 这是另一个例子:

    $empty_currency = array('empty'=>'(choose a currency)');
    ...
    echo "<table class=\"all order\"><tr><td>\n";
    echo $this->Form->input('tax_currency_id', $empty_currency);
    echo "</td></tr></table>\n";
    echo "<table><tr><td colspan='2' class='all order receive'>\n";
    echo $this->Form->input('shipping_comment');
    echo "</td></tr><tr class='all order costing'><td>\n";
    echo $this->Form->input('shipping_cost');
    echo "</td><td>\n";
    echo $this->Form->input('shipping_currency_id', $empty_currency);
    echo "</td></tr></table>\n";

Sometimes if you haven't followed naming conventions perfectly then you will have to specify array(...'type'=>'select') in your input call. 有时,如果您没有很好地遵循命名约定,则必须在输入调用中指定array(...'type'=>'select') Here's an example: 这是一个例子:

    echo $this->Form->input('budget_year_dflt', array('type'=>'select', 'options'=>$budgetYears, 'label'=>'Default Budget Year'));

If you're new to cakephp, do yourself a favor and THOROUGHLY read and understand the naming conventions before you do your table design and well before you start developing. 如果您是Cakephp的新手,请帮自己一个忙,在进行表设计之前以及开始开发之前,请彻底阅读并理解命名约定。 Life is so easy if you follow their conventions and so hard if you don't... 如果遵循他们的约定,生活是如此轻松,而如果您不遵循约定,那么生活将会如此艰难……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM