我刚刚学习 yii(一周),我有两个捕获月份和年份的下拉列表,但我需要使用连接的月份和年份并通过 submitButton 完成搜索,我该怎么做?
我的代码:
查看:
<?php echo $form->dropDownList($model,'mes',CHtml::Listdata(Meses::model()->findAll(),'mesID','mesNom'), array('class'=>'form-control','prompt'=>'--Selección--')); ?>
<?php echo $form->error($model,'fecha',array('class'=>'alert alert-danger')); ?>
<?php echo $form->dropDownList($model,'anio',CHtml::Listdata(Anios::model()->findAll(),'anioID','anio'), array('class'=>'form-control','prompt'=>'--Selección--')); ?>
<?php echo $form->error($model,'anio',array('class'=>'alert alert-danger'));
?>
<?php echo CHtml::submitButton('Buscar', array('class'=>'btn btn-success')); ?>
我如何在 Controller 中做到这一点? 请帮助我。
最佳答案
您可以简单地通过从提交中获取值(value)并过滤条件的数据依赖性来做到这一点,如下所示:
public function actionYearMonthSearch()
{ $month = 01; $year = 2018; // Default values
// get values
if(isset($_GET['month']))
$month= $_GET['month'];
if(isset($_GET['year']))
$year=$_GET['year'];
// You can concat both by $concat = $year.'-'.$month; and then using it nested of month and year like updated_at => $concat....etc
$model = YourModel::model()->findAllByAttributes(array(
'month' => $month,
'year' => $year,
), array(
'limit' => 5,
));
// ... and any code you need too ...
}
您需要阅读 Yii documentation看看actions , 还有 active record
关于php - 如何在 php (Yii) 中连接两个下拉列表并搜索连接结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53475027/