yii 多条件搜素

原生搜素也可以搜所:必免了搜素时跳到首页必需时get
<form action="?r=upload ow" method="get">
    <input type="hidden" name="r" value="upload ow">//重点
    <input type="text" name="search"/><input type="submit" value="搜索"/>
</form>



视图:

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\helpers\Url;
use yii\base;
//必须引入否则报错
//Class 'ArrayHelper' not found
use yii\helpers\ArrayHelper;
use\yii\widgets\LinkPager;
?>
<?php
$form=ActiveForm::begin([
    'action'=>Url::toRoute(['showc']),
    'method'=>'get',
]);
echo '姓名:'.Html::input('text','username',$search);//可以写第三个值,相当于value保留值,但是添加成功不显示&username,能力强大的人可以解决一波

//echo '学历:'.Html::activeDropDownList($xue,'xueli',ArrayHelper::map($xueli,'id','xueli'),['style'=>'border:1px solid    gray;width:150px;']);;//可以写第三个值,相当于value保留值,但是添加成功不显示&username,能力强大的人可以解决一波

echo $form->field($xue,'xueli')->dropDownList(ArrayHelper::map($xueli,'id','xueli'),['style'=>'border:1px solid blue;width:150px;']);

echo Html::submitButton();
ActiveForm::end();echo "<br/>";
?>

控制器:
    public function actionShowc(){
//        print_r($_GET);die;
        $db=\Yii::$app->db;
//        $count=$db->createCommand("select COUNT(*) from ceshi")->queryScalar();
//        $pages= new Pagination(['totalCount'=>$count,'pageSize'=>3]);
//        $date=$db->createCommand("select * from ceshi")->queryAll();
//        echo $_GET['Ceshixueli']['xueli'];die;
        $user=empty($_GET['username'])?'':$_GET['username'];
        $xueli=empty($_GET['Ceshixueli']['xueli'])?'':$_GET['Ceshixueli']['xueli'];
//        print_r($user);die;
        if(!empty($user) or !empty($xueli)){
            $query = Ceshi::find()->where("username like '%$user%'")->andwhere([
                'or',
                ['like','xueli',$xueli]]);
        }else{
            $query = Ceshi::find();
        }

        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize'=>3]);
        $date = $query->offset($pages->offset)
            ->limit($pages->limit)
            ->all();
       $xue=new Ceshixueli();
        $xuel=$xue->find()->asArray()->all();
//        print_r($xuel);die;
        $xuel=$db->createCommand("select * from ceshixueli")->queryAll();
        //把另一个表里的值循环添加到另一个表中
//        print_r($date);die;
        $x=array();
        foreach($date as &$val){
            foreach($xuel as &$va){
                if($val['xueli']==$va['id']){
                    $val['xueli']=$va['xueli'];
                   $x[$val['id']]=$va['xueli'];
                }
            }
        }

//        foreach(){}
//        print_r($date);die;
        return $this->render('showc',['date'=>$date,'xue'=>$xue,'xueli'=>$xuel,'search'=>$user,'pages'=>$pages]);
    }




详细介绍yii2下拉框的实现方法,以商品分类的下拉框为例:

第一种方法:使用Html的activeDropDownList(),该方法的优点是:可以自定义下拉框的样式。具体实现如下:

1.控制器中,获取视图中所需的数据

    public function actionList(){
        $category_model=new Category();
        $category_info=$category_model->find()->all();
        return $this->renderPartial('list',['category_info'=>$category_info,'category_model'=>$category_model]);
    }

2.视图中,use yii\helpers\Html;

<?php $form = ActiveForm::begin([]);?>
    <?php echo Html::activeDropDownList($category_model,'category_name',ArrayHelper::map($category_info,'category_id','category_name'),['style'=>'border:1px solid    gray;width:150px;'])?>           
<?php ActiveForm::end(); ?>

3.这样之后,表单的下拉框名称是:Category Name,如果要改变这个值,在控制器对应的模型中使用attributeLabels()方法,具体如下:

 

  public function attributeLabels(){
        return array(
            'category_name'=>'分类',
        );
    }

第二种方法:使用ActiveForm的dropDownList(),优点是使用的yii自定义的样式,也可以自定义样式

1.控制器中获取视图中需要的数据

2.视图中,use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;

<?php $form = ActiveForm::begin([]);?>
    <?php echo $form->field($category_model,'category_name')->dropDownList(ArrayHelper::map($category_info,'category_id','category_name'),['style'=>'border:1px solid blue;width:150px;'])?>           
<?php ActiveForm::end(); ?>

补充:该方法也可以自定义样式:具体如上代码

注意:如果要在下拉框的首项添加‘请选择’、‘全部’等默认的值。用prompt,代码如下:

 <?php echo $form->field($brand_model,'brand_name')->dropDownList(ArrayHelper::map($brand_info,'brand_id','brand_name'),['prompt'=>'全部'])?>  

各参数说明:

ArrayHelper::map()------------生成k=>v的一维或多维数组

$category_info----------------后台获取的数据

category_id-------------------option的value值

category_name---------------select 的name值



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值