从 cakephp 的下拉菜单中获取选定的值 2.x
get the selected value from drop down menu in cakephp 2.x
我正在尝试从下拉菜单中选择。但它 returns 所选元素的索引。我想检索该索引的值。我想获得 $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;
我正在尝试从下拉菜单中选择。但它 returns 所选元素的索引。我想检索该索引的值。我想获得 $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;