zhangchi
- zhangchi 收藏了问题yii2 AR搜索分页
- zhangchi 赞了回答
长尾词
view
<?php $form = ActiveForm::begin(['action' => ['work/work_show'],'method'=>'get',]); ?>
<?php ActiveForm::end(); ?>
//向服务器上的search发送post数据,$.post是jQuery的方法 $.post('?r=work/work_search',{'value':val},function(data){ if(data=='0'){ //判断服务器上返回的数据,如果等于0,则表示没有找到相关的内容, //所以将提示框的内容清空并隐藏 $('#search_auto').html('').css('display','none'); }else{ //如果服务器上返回的数据不等于0,则将返回的内容放到提示框内并显示提示框 $('#search_auto').html(data).css('display','block'); } }); $('#search_auto').css({'width':$('#search input[name="search_key"]').width()+6});
}
controller
public function actionWork_search(){ $value = \Yii::$app->request->post('value'); $model = new Job(); $list = $model->find()->where(['like','JName',$value])->asArray()->all(); if(empty($list)){ echo 0;die; } $str=''; $str.= '<ul>'; foreach ($list as $v){ $str.= '<li><a href="?r=work/work_show&search_key='.$v['JName'].'">'.$v['JName'].'</a></li>'; //将查询得到的相关结果的标题输出,这个地方需要注意,由于通过jQuery的ajax技术返回的文本 //是UTF-8编码,所以如果$v['position'] 中包含中文,一定要记得用PHP的iconv或其它函数将其转换 //成UTF-8编码,否则在页面中看到的会是一串乱码 } $str.= '<li class="cls"><a href="javascript:;" onclick="$(this).parent().parent().parent().fadeOut(100)">关闭</a></li>'; //输入一个关闭按钮,让用户不想看到提示层时可以点击关闭,关闭按钮采用jQuery,解释一下, //当前点击的按钮是$(this),一直向上找到其第三个父元素,让它逐渐消失 $str.= '</ul>'; echo $str;die; }
- zhangchi 赞了回答
验证码
controller public function actions(){ return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, 'backColor'=>0x000000,//背景颜色 'maxLength' => 6, //最大显示个数 'minLength' => 5,//最少显示个数 'padding' => 5,//间距 'height'=>40,//高度 'width' => 130, //宽度 'foreColor'=>0xffffff, //字体颜色 'offset'=>4, //设置字符偏移量 有效果 //'controller'=>'login', //拥有这个动作的controller ], ]; } model public $verifyCode; public function rules(){ return [ ['verifyCode', 'captcha'] ]; } public function attributeLabels(){ return [ 'verifyCode' => 'Verification Code', ]; } view use yii\captcha\Captcha; ?= $form->field($model, 'verifyCode')->textInput(['class'=>'dl_textinp'])->label('') ?> <?=Captcha::widget(['name'=>'captchaimg','captchaAction'=>'site/captcha', 'imageOptions'=>['id'=>'captchaimg', 'title'=>'换一个', 'alt'=>'换一个', 'style'=>'cursor:pointer;margin-left:25px;'],'template'=>'{image}']);?>
- zhangchi 赞了回答
文件上传 多文件
`控制器 yii\web\UploadedFile; $model->file = UploadedFile::getInstances($model, 'file'); $file_arr = array(); foreach($model -> file as $file){ $file_name = 'uploads/' . time() . rand(111111,999999).'.'. $file->extension; $file -> saveAs($file_name); $file_arr[] = $file_name; } 视图: <?php use yii\widgets\ActiveForm; use yii\helpers\Html; ?> <?php $form=ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data'],'action'=> ['news/upfile'],'method'=>'post',])?> <?=$form->field($model,'file[]')->fileInput(['multiple'=>true])?> <?php echo Html::submitButton('提交',['class' => 'btn btn-primary'])?> <?php ActiveForm::end();?>
`
- zhangchi 赞了回答
``` 首先引入 yii\data\Pagination; 还有各种模型 控制器: new一个AR对象出来 $search = Yii::$app->request->get('search_key'); $where = '1 = 1'; if ($search) { $where .= ' and JName like "%'.$search.'%"'; } $pageArr = $model -> find()->where($where); $pagination = new Pagination(['totalCount' => $pageArr -> count(),'pageSize'=>'3']); $result = $pageArr ->join('INNER JOIN', 'recruit_enterprise','recruit_enterprise.CId = recruit_job.CId') ->select('*') ->offset($pagination ->offset) ->limit($pagination ->limit) ->where($where) ->asArray() ->all(); return $this -> render('job_list', ['result' => $result, 'pagination' => $pagination, ] );
以上是本人个人代码 仅供参考