yii 图片上传 搜索分页

来源:互联网 发布:php中文验证码识别 编辑:程序博客网 时间:2024/06/15 13:19
  1. 访问路径:http://www.sch.com/yii/advanced/backend/web/index.php?r=lxa/list
  2. use backend\models\Lxaform; //小部件
  3. use backend\models\Lxa;//主表
  4. use backend\models\Lxacate;//副表
  5. use backend\models\Upload;//图片上传
  6. advanced/frontend/web/uploads //文件存储

LxaController.php

<?phpnamespace backend\controllers;use yii\web\Controller;use yii;use db;use backend\models\Lxaform;  //小部件use backend\models\Lxa;//主表use backend\models\Lxacate;//副表use backend\models\Upload;//图片上传 use yii\data\Pagination;use yii\web\UploadedFile; class LxaController extends Controller{    //表单    public function actionForm(){        $model = new Lxaform(); //小部件        $models = new Upload();            $data = Lxacate::find()->asArray()->all();        $arr = Lxaform::dataarr($data);         return $this->render('form',['model'=>$model,'models'=>$models,'data'=>$arr]);    }       //添加    public function actionAdd(){        $model = new Upload();        if (Yii::$app->request->isPost)         {            //$model->book_pic = UploadedFile::getInstance($model, 'book_pic');  //单文件            // $model->imageFile = UploadedFile::getInstances($model, 'imageFile');//多文件            $model->story = UploadedFile::getInstance($model, 'story');  //单文件            if ($path2 = $model->upload2()) {                     echo  $path2;            }            }        $story=$path2;         // die;        $post = Yii::$app->request->post('Lxaform');        $lxa = new Lxa();        $lxa->title = $post['title'];        $lxa->content = $post['content'];        $lxa->cate_id = $post['cate_id'];        $lxa->story = $story;//图片        $res=$lxa->save();        if($res){            echo '<script>alert("添加成功");            location.href="?r=lxa/list"</script>';        }    }    //搜索        public function actionList()        {            $model = new Lxaform();  //小部件            $models=new Lxa;//主表            $where=" 1=1 ";            $sou1='';            $sou2='';            if($sou1 = Yii::$app->request->get('Lxaform')['sou1'])               $where .=" and title like '%$sou1%' ";            if($sou2 = Yii::$app->request->get('Lxaform')['sou2'])               $where .=" and cate_name like '%$sou2%' ";            $count=Lxa::find()->joinWith('lxacate')->where($where)->count();  //总条数            $page=  Yii::$app->request->get('page') ?  Yii::$app->request->get('page') : 1;               //当前页            $pageSize=5;               $limit=($page-1)*$pageSize;  //偏移量            $data=$models->find()                ->joinWith('lxacate')  //副表                ->select('lianxi0523.*,lianxi05231.cate_name')                  ->where($where)                ->offset($limit)                ->limit($pageSize)                ->asArray()                ->all();                        $pages = new Pagination(['totalCount' => $count,'pageSize'=>$pageSize]);                    return $this->render("list",['model'=>$model,'list'=>$data,'sou1'=>$sou1,'sou2'=>$sou2,'pages'=>$pages]);        }    //删除    public function actionDel()    {        $id = Yii::$app->request->get('id');        $exam = Lxa::findOne("$id");        $res=$exam->delete();        if($res){        echo '<script>alert("删除成功");        location.href="?r=lxa/list"</script>';        }    }    //修改            public function actionUp()    {        $id = Yii::$app->request->get('id');        $query = Lxa::find()                ->joinWith('lxacate')                  ->select('lianxi0523.*,lianxi05231.cate_name')                ->where("id=$id")                  ->asArray()                ->all();        $model = new Lxaform();   //小部件             $data = Lxacate::find()->asArray()->all();//副表        $arr = Lxaform::dataarr($data);        return $this->render('up',['model'=>$model,'list'=>$query,'data'=>$arr]);    }    public function actionUp_do()    {        $post = Yii::$app->request->post('Lxaform');        $id=$post['id'];        $title=$post['title'];        $content=$post['content'];        $cate_id=$post['cate_id'];        $exam = Lxa::findOne("$id");        $exam->title = $title;        $exam->content = $content;        $exam->cate_id = $cate_id;        $res=$exam->save();        if($res){        echo '<script>alert("修改成功");        location.href="?r=lxa/list"</script>';        }    }}

models

Lxa.php

<?php namespace backend\models;use yii\db\ActiveRecord;class Lxa extends ActiveRecord{    public static function tableName(){        return 'lianxi0523';    }    public function getLxacate(){          return $this->hasMany(Lxacate::className(), ['cate_id' => 'cate_id']);      } }

Lxacate.php

<?php namespace backend\models;use yii\db\ActiveRecord;class Lxacate extends ActiveRecord{    public static function tableName(){    return 'lianxi05231';    }}

Lxaform

<?phpnamespace backend\models;use yii\base\Model;use yii\widgets\ActiveForm;use yii\helpers\Html;class Lxaform extends Model{    public $id;    public $title;    public $content;    public $cate_id;    public $sou1;    public $sou2;    public function rules(){        return[        ];    }    public function attributeLabels(){        return[        'title'=>'标题',        'content'=>'描述',        'cate_id'=>'分类',        ];    }    //分类    static public function dataarr($data){        $arr = array();        foreach($data as $key=>$value){        $arr[$value['cate_id']] = $value['cate_name'];        }        return $arr;    }}

Upload.php

<?phpnamespace backend\models;use yii\base\Model;use yii\widgets\ActiveForm;use yii\helpers\Html; use yii\web\UploadedFile;class Upload extends Model{    public $book_pic;    public $story;    public function rules()    {        return [            // [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],            //['book_pic','file','maxSize'=>1024*1024*10,'minSize'=>1024*50],            ['story','file','maxSize'=>1024*1024*10,'minSize'=>1024*1],            // ['story','file','maxSize'=>1024*1024*10,'minSize'=>1024*50,'maxFiles' => 4]        ];    }    public function upload()    {        if ($this->validate()) {            $this->book_pic->saveAs('uploads/' . $this->book_pic->baseName . '.' . $this->book_pic->extension);            $filePath = 'uploads/' . $this->book_pic->baseName . '.' . $this->book_pic->extension;            return $filePath;        } else {            return false;        }    }    public function upload2()    {        if ($this->validate()) {            $this->story->saveAs('uploads/' . $this->story->baseName . '.' . $this->story->extension);            $filePath = 'uploads/'. $this->story->baseName . '.' . $this->story->extension;            return $filePath;        } else {            return false;        }    }    public function upload3()    {        //多文件        $filePath="";        if ($this->validate())         {             foreach ($this->imageFile as $file)             {                $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);                $filePath .=','. 'uploads/' . $file->baseName . '.' . $file->extension;            }            return $filePath;        } else {            return false;        }    }}

views

form.php

<?phpuse yii\helpers\Html;use yii\widgets\ActiveForm;$form = ActiveForm::begin(['id' => 'login-form','options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],'action'=>'?r=lxa/add','method'=>'post',]) ?><?= $form->field($model, 'title')->textInput() ?><?= $form->field($model, 'content')->textarea() ?><?= $form->field($model, 'cate_id')->dropDownList($data,['prompt'=>'亲,选择分类咯']) ?><?= $form->field($models, 'story')->fileInput(['multiple' => true])   ?><div class=”form-group”><div class=”col-lg-offset-1 col-lg-11″><?= Html::submitButton('submit', ['class' => 'btn btn-primary']) ?></div></div><?php ActiveForm::end() ?>

list.php

<?phpuse yii\helpers\Html;use yii\widgets\ActiveForm;use yii\widgets\LinkPager;$form = ActiveForm::begin(['id' => 'login-form','options' => ['class' => 'form-horizontal'],'action'=>'?r=lxa/list','method'=>'get',]) ?><?= $form->field($model, 'sou1')->textInput(['value'=>$sou1]) ?><?= $form->field($model, 'sou2')->textInput(['value'=>$sou2]) ?><div class=”form-group”><div class=”col-lg-offset-1 col-lg-11″><?= Html::submitButton('sou', ['class' => 'btn btn-primary']) ?></div></div><?php ActiveForm::end() ?><center>    <table border=1>        <tr>            <th width="100px;">ID</th>            <th width="100px;">标题</th>            <th width="100px;">描述</th>            <th width="100px;">分类</th>            <th width="200px;">图片</th>            <th width="100px;">操作</th>        </tr>        <?php foreach ($list as  $v): ?>        <tr>            <td><?php echo $v['id'] ?></td>            <td><?php echo $v['title'] ?></td>            <td><?php echo $v['content'] ?></td>            <td><?php echo $v['cate_name'] ?></td>            <td> <img src="<?php echo $v['story'] ?>" width="100px"> </td>            <td>                <a href="?r=lxa/del&id=<?php echo $v['id'] ?>">删除</a>                <a href="?r=lxa/up&id=<?php echo $v['id'] ?>">修改</a>            </td>        </tr>        <?php endforeach ?>    </table>    <?= LinkPager::widget(        [        'pagination' => $pages,        'maxButtonCount'=>2,        'firstPageLabel' => '首页',        'lastPageLabel' => '末页',       ]) ?></center>

up.php

<?phpuse yii\helpers\Html;use yii\widgets\ActiveForm;$form = ActiveForm::begin(['id' => 'login-form','options' => ['class' => 'form-horizontal'],'action'=>'?r=lxa/up_do','method'=>'post',]) ?><?= $form->field($model, 'id')->hiddenInput(['value'=>$list[0]['id']]) ?><?= $form->field($model, 'title')->textInput(['value'=>$list[0]['title']]) ?><?= $form->field($model, 'content')->textarea(['value'=>$list[0]['content']]) ?><?= $form->field($model, 'role')->textInput(['value'=>$role,'readonly'=>'true','hidden','style' => 'background:#EED;font-style:italic']) ?><?php $model->cate_id = $list[0]['cate_id']; ?><?= $form->field($model, 'cate_id')->dropDownList($data,['prompt'=>'亲,选择分类咯']) ?><div class=”form-group”><div class=”col-lg-offset-1 col-lg-11″><?= Html::submitButton('update', ['class' => 'btn btn-primary']) ?></div></div><?php ActiveForm::end() ?>
原创粉丝点击