2011-07-29 34 views
0

我有一個國家名單助手實施得很好,可以選擇相當愉快。國家下拉列表輸出查看

有一兩件事我想現在制定的是如何將所選擇的國家,查看...

樣的幫手:

class CountryListHelper extends FormHelper { 

    var $helpers = array('Form'); 

function select($fieldname) { 
$list = $this->Form->input($fieldname , array(
    'type' => 'select', 'label' => 'Country of Residence', 'options' => array(
     '' => 'Please select a country', 
     'AF' => 'Afganistan', 
     'AL' => 'Albania', 
     'DZ' => 'Algeria', 
     'AS' => 'American Samoa', 
     'AD' => 'Andorra', 
     'AO' => 'Angola', 
     'AI' => 'Anguilla', 
     'AQ' => 'Antarctica', 

在編輯代碼,並添加觀點:

echo $this->CountryList->select('country'); 

被存儲的數據是隻有縮寫(如圖輔助代碼片段),這就是被輸出到view.ctp(AF例如)。 有沒有辦法從助手做一個檢索,以匹配完整的國家名稱的縮寫,並推到view.ctp?

從view.ctp片段,我試圖修改顯示完整的國家名稱只適用於首字母縮寫詞。

<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Country of Residence'); ?></dt> 
     <dd<?php if ($i++ % 2 == 0) echo $class;?>> 
      <?php echo $user['User']['country']; ?> 
      &nbsp; 
     </dd> 

非常感謝提前!從圖

回答

1
class CountryListHelper extends AppHelper { 
    var $helpers = array('Form'); 
    var $countries = array(
    'AF' => 'Afganistan', 
    'AL' => 'Albania', 
    'DZ' => 'Algeria', 
    ...) 
function getCountry($country){ 
    return $this->countries[$country]; 
} 
function select($fieldname){ 
    $list = $this->Form->input($fieldname , array(
    'type' => 'select', 'label' => 'Country of Residence', 'options' => $countries) 

電話:

echo $this->CountryList->getCountry($user['User']['country']); 
+0

不太什麼我問了 - 由於沒有少 – Plastika

+0

不知道我在寫:))固定。另外,您應該從AppHelper擴展。 –

+0

非常感謝!最後可以停止出汗這一個.. – Plastika