从cakephp 2.x的下拉菜单中获取选定的值

时间:2019-04-23 06:18:34

标签: drop-down-menu 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'=>'')
    ));
  }
}

1 个答案:

答案 0 :(得分:0)

对于年份中的从零开始的索引,似乎永远不会有任何有用的事情,而您只想要实际的年份值。在这种情况下,请设置选项数组,使索引和值相同:

$years[$year]=$year;