php - 如何在 php (Yii) 中连接两个下拉列表并搜索连接结果?

标签 php html yii

我刚刚学习 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/

相关文章:

php - 如何根据评分和发布时间找到最佳帖子?

php - 如何使用jquery获取用户时区?

html - css 链接图片只能点击一部分

html - 仅当元素具有具有特定类的子元素时才选择该元素

php - 如何在表单提交时每次使用一个对象?

PHP 邮件编码主题行

html - 元素周围的 css 剪切边框/形状

javascript - 无法包含 JavaScript

Yii 2 根据连接表中的值过滤多对多关系

php - SQL 语句中文字前的冒号是什么意思?