小超
- 小超 收藏了问题yii2 AR搜索分页
- 小超 赞了回答
文件上传 多文件
`控制器 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();?>
`
文件上传 多文件
`控制器 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();?>
`
@林小陌 public function actionSoso(){
$model = new Fuxi(); $uname = $_GET['uname']; $content = $_GET['content']; $where = '1=1'; if(!empty($uname)){ $where.=" and uname like '%".$uname."%'"; } if(!empty($content)){ $where.=" and content like '%".$content."%'"; } $pagination = new Pagination([ 'defaultPageSize' => 5, 'totalCount' => $model->find()->count(), ]); $list = $model->find() ->offset($pagination->offset) ->limit($pagination->limit) ->where($where) ->asArray()->all(); //print_r($list);die; return $this->render('show',['list'=>$list,'pagination'=>$pagination,'uname'=>$uname,'content'=>$content]); } public function actionXiazai(){ print_r($_SERVER);die; $url = 'http://localhost/Yii/yii/advanced/frontend/web/index.php?r=hello/show'; $list = file_get_contents($url); $filename = rand(0,100).".doc"; $myfile = fopen($filename, "w"); fwrite($myfile, $list); $this->redirect("http://localhost/Yii/yii/advanced/frontend/web/".$filename); }
``` 首先引入 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, ] );
以上是本人个人代码 仅供参考
@林小陌 public function actionAdd(){
$model = new Fuxi(); $db = Yii::$app->db; if (Yii::$app->request->isPost) { $files = $model->file = UploadedFile::getInstances($model, 'file'); foreach($files as $val){ $list = $val->saveAs('../web/uploads/' . $val->baseName . '.' . $val->extension); } if($list==1){ foreach($files as $v){ $img = $v->name; } $res = $db->createCommand()->batchInsert('fuxi',['uname','content','file'],[[$_POST['Fuxi']['uname'],$_POST['Fuxi']['content'],$img]])->execute(); if($res==1){ $this->redirect(array('hello/show')); } }else{ echo '上传失败'; } } }