从 CakePHP 2.x 的下拉菜单中获取所选值



我正在尝试从下拉菜单中获取所选元素。 但它返回所选元素的索引。我想检索该索引的值。我想获取$year的值。如何获取值?

生成年份的控制器

<?php
class StockReportConditionsController extends AppController{
  public function report() {
    $years = array();
    for ($i = 0; $i < 4; $i++) {
      $year = date("Y");
      $year = $year -$i;
      $years[$i]=$year;
      }
      $this->set(compact('years'));
}

视图

<h1>Weekly Report</h1>
<table>
    <tr>
        <th>Year</th>
        <th>Stores</th>
        <th>Department</th>
    </tr>
    <?php echo $this->Form->create('StockReport',array('url'=>array('type' => 'get','action'=>'search')));?>
<tr>
  <td>
    <?php echo $this->Form->input('year',array('type'=>'select','options'=>$years)); ?>
</td>
<td>
  <?php echo $this->Form->input('Store.name',array('type'=>'select','options'=>$stores)); ?>
</td>
<td>
  <?php echo $this->Form->input('StockReportCondition.bu_no',array('type'=>'select','options'=>$departments)); ?>
</td>
</tr>
<?php echo $this->Form->end(__('Search'));?>
</table>

控制器

<?php
class StockReportsController extends AppController{
  public function search(){
    $year = $this->request->data['StockReport']['year'];
    echo $year;
    $store_no = $this->request->data['Store']['name'];
    echo $store_no;
    $dept_no = $this->request->data['StockReportCondition']['bu_no'];
    echo $dept_no;
    $result = $this->StockReport->find('all',array(
        'conditions'=>array('yearweek'=>'', 'bu_no'=>'','tn_no'=>'')
    ));
  }
}
似乎从来没有

有用的事情可以处理年份的零开始索引,并且您只需要实际的年份值。如果是这种情况,则设置选项数组,使索引和值相同:

$years[$year]=$year;

最新更新