数据修改查询增加小结

Original 2018-11-07 10:32:33 159
abstract:数据修改【根据所选择的内容不同ID也不同进行判断】要更新的是那一篇内容数据【id】传参形式//F1-EG {:url(\'News/edit\')}?id={$news.id}[\为转义符在需要的时候添加即可] //F2-EG {:url('News/edit',['id'=>$conf.id])}判断是否是post提交及获取全部的post数据

数据修改【根据所选择的内容不同ID也不同进行判断】要更新的是那一篇内容

数据【id】传参形式

//F1-EG
{:url(\'News/edit\')}?id={$news.id}[\为转义符在需要的时候添加即可]
//F2-EG
{:url('News/edit',['id'=>$conf.id])}

判断是否是post提交及获取全部的post数据方法

//F1-EG【控制器默认封装的方法,只需引入controller即可】

request()->isPost();
$data = input('post.');

//F2-EG[需引入Request静态代理]

Request::isPost();
$data = Request::param();

添加操作

public function add()
    {
        if(Request::isPost())
        {
            $data = Request::param();
            $newstitid = $data['newstitid'];
            $res = ThumbM::where('newstitid',$newstitid)->find();//判断标题是否重复
            if($res){
                return ['msg'=>'新闻标题已经有缩略图了,不能重复添加哦'];
            }
            $data['username'] = Session::get('name');
            $data['time'] = time();
            $thb = new  ThumbM;
            if($thb->save($data))//判断数据是否插入成功
            {
                return ['res'=>1,'msg'=>'新闻缩略图添加成功'];
            }else{
                return ['msg'=>'新闻缩略图添加失败'];
            }
        }
        $xwtit = News::field('id,title')->select(); 
        $this->view->xwtit = $xwtit;
        return view();   
    }

修改查询操作

public function edit()
    {
        $id = Request::param('id');
        $newsres = NewsM::where('id',$id)->find();
        $this->view->newsres = $newsres;//根据id給修改模板付数据库存在的初始值
        $fenlei = Classify::field('newsflname,id')->select();
        $this->view->fenlei = $fenlei;//由于新闻添加模板有新闻分类,显示默认分类用
        return view();
    }

    public function DoEdit()
    {
        $data = Request::param();
        $data['username'] = Session::get('name');
        $data['time'] = time();
        $news = new NewsM;
        $num = $news->save($data,['id' => $data['id']]);
        if(is_numeric($num)){
            return ['res'=>1,'msg'=>"修改成功{$num}条内容"];
        }else{
            return ['res'=>0,'msg'=>'修改失败'];
        }
    }

新闻分类显示已经选择的分类

<div class="layui-form-item">
  <label for="username" class="layui-form-label">
      <span class="x-red">*</span>新闻分类
  </label>
  <div class="layui-input-inline">
      <select id="shipping" name="shipping" class="valid">
        
        {volist name='fenlei' id='fl'}
        <option {if $newsres.classify eq $fl.id} selected {/if} value="{$fl.id}">{$fl.newsflname}</option>
        {/volist}

      </select>
  </div>
</div>


Correcting teacher:天蓬老师Correction time:2018-11-07 11:11:34
Teacher's summary:作业完成不错,最好能区分开控制器和视图,方便老师阅读

Release Notes

Popular Entries