CakePHP:如何从数据库创建 OptGroup 下拉列表?
CakePHP : How to Create OptGroup Drop down list from DataBase?
我是 CakePHP 的新手,正在开发一个新项目。我只想知道,如何显示从数据库中获取的 optgroup 下拉列表。
我想像下面这样获取数组
$arr = array(
'optgroup' => array(
'1','2','3'),
'optgroup2' => array(
'1','2','3')
);
我假设您已经设置好模型。
在您的控制器中,您需要将变量分配给 find(list)
调用的结果,然后 set()
它们。这是一个项目的示例,其中我有请求、货币等的下拉列表:
$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'));
(您也可以通过一系列 $this->set(...)
调用而不是使用变量和 $this->set(compact(...))
。)
然后在您看来,您使用 Form->input()
调用来生成 select 语句。这是另一个例子:
$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";
有时,如果您没有完全遵循命名约定,则必须在输入调用中指定 array(...'type'=>'select')
。这是一个例子:
echo $this->Form->input('budget_year_dflt', array('type'=>'select', 'options'=>$budgetYears, 'label'=>'Default Budget Year'));
如果您是 cakephp 的新手,请帮自己一个忙,在进行 table 设计和开始开发之前仔细阅读并理解命名约定。如果你遵循他们的惯例,生活会很轻松,如果你不遵守,生活会很艰难...
我是 CakePHP 的新手,正在开发一个新项目。我只想知道,如何显示从数据库中获取的 optgroup 下拉列表。
我想像下面这样获取数组
$arr = array(
'optgroup' => array(
'1','2','3'),
'optgroup2' => array(
'1','2','3')
);
我假设您已经设置好模型。
在您的控制器中,您需要将变量分配给 find(list)
调用的结果,然后 set()
它们。这是一个项目的示例,其中我有请求、货币等的下拉列表:
$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'));
(您也可以通过一系列 $this->set(...)
调用而不是使用变量和 $this->set(compact(...))
。)
然后在您看来,您使用 Form->input()
调用来生成 select 语句。这是另一个例子:
$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";
有时,如果您没有完全遵循命名约定,则必须在输入调用中指定 array(...'type'=>'select')
。这是一个例子:
echo $this->Form->input('budget_year_dflt', array('type'=>'select', 'options'=>$budgetYears, 'label'=>'Default Budget Year'));
如果您是 cakephp 的新手,请帮自己一个忙,在进行 table 设计和开始开发之前仔细阅读并理解命名约定。如果你遵循他们的惯例,生活会很轻松,如果你不遵守,生活会很艰难...