[英]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的新手,請幫自己一個忙,在進行表設計之前以及開始開發之前,請徹底閱讀並理解命名約定。 如果遵循他們的約定,生活是如此輕松,而如果您不遵循約定,那么生活將會如此艱難……
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.