2014-03-12 37 views
0

我的代碼是像下面的東西:如何使用cakephp獲取dropdownlist中的選定值?

public function makeEntryTime($first = '') { 
     $j = 0; 
     if (empty($first)) { 
      $j = 1; 
     } else { 
      $entry[$j] = $first; 
      $j++; 
     } 

     for ($i = $j; $i <= 12; $i++) { 
      $entry[$i . ' am'] = $i . ' am'; 
     } 
     for ($i = $j; $i <= 12; $i++) { 
      $entry[$i . ' pm'] = $i . ' pm'; 
     } 
     return $entry; 
    } 

這裏是下拉列表代碼:

$this->Form->select('ClubOpenDay.0.open_time', $this->makeEntryTime(), array("empty" => false, 'class' => 'input-medium')); 

我的問題是我得到的價值像上午11時,12 am.But我想當我從數據庫上午11點或上午12點獲得價值時選擇它。任何想法我該如何做到這一點?

+0

爲什麼不使用[**'FormHelper :: dateTime()'**](http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html #FormHelper :: dateTime)或[**'FormHelper :: input()'**](http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::put )與'時間'類型? PS。請始終提及您的確切CakePHP版本! – ndm

回答

0

請試試這個:

<?php echo $this->Form->select('ClubOpenDay.0.open_time',$this->makeEntryTime(),array("empty" => false,'value' => $value_selected,'class' => 'input-medium')); ?> 

「$ value_selected」 是與輸入範圍內的任意值。

即,$ this-> makeEntryTime()返回。

謝謝

0

我假設你的方法是在視圖中,你不應該這樣做。從你的函數中刪除公共,並刪除$這個來調用你的函數。

儘量讓你的方法至少在控制器內。這裏是如果你想保持你的方法在視圖中。

function makeEntryTime($first = '') { 
     $j = 0; 
     if (empty($first)) { 
      $j = 1; 
     } else { 
      $entry[$j] = $first; 
      $j++; 
     } 

     for ($i = $j; $i <= 12; $i++) { 
      $entry[$i . ' am'] = $i . ' am'; 
     } 
     for ($i = $j; $i <= 12; $i++) { 
      $entry[$i . ' pm'] = $i . ' pm'; 
     } 
     return $entry; 
    }   

    echo $this->Form->select('selete_id', makeEntryTime(), array("empty" => false, 'class' => 'input-medium')); 

但如果你想使它MVC。

//controller 
     function makeEntryTime($first = '') { 
      $j = 0; 
      if (empty($first)) { 
       $j = 1; 
      } else { 
       $entry[$j] = $first; 
       $j++; 
      } 

      for ($i = $j; $i <= 12; $i++) { 
       $entry[$i . ' am'] = $i . ' am'; 
      } 
      for ($i = $j; $i <= 12; $i++) { 
       $entry[$i . ' pm'] = $i . ' pm'; 
      } 
      return $entry; 
     } 

//page function - for examlpe add() method 
     public function add(){ 
      $this->set('times', $this->makeEntryTime()); 
     } 

//view of add method  
     echo $this->Form->select('time_id', $times, array("empty" => false, 'class' => 'input-medium')); 
相關問題