文章相关操作

1.验证问题解决以及文章表创建和页面的搭建

(1)验证问题的解决

<?php
//修改栏目代码
      public function edit(){
        if(request()->isPost()){
            $data=[
                'id'=>input('id'),
                'catename'=>input('catename'),
                'keywords'=>input('keywords'),
                'desc'=>input('desc'),
                'type'=>input('type'),
            ];
            $validate = \think\Loader::validate('Cate');
            if($validate->check($data)){
                if($db=\think\Db::name('cate')->update($data)){
                    return $this->success('修改栏目成功!','lst');
                }else{
                    return $this->error('修改栏目失败!');
                }
            }else{
                return $this->error($validate->getError());
            }
            return;
        }
        $id=input('id');
        $cates=db('cate')->where('id',$id)->find();
        $this->assign('cates',$cates);
        return $this->fetch();
    }
  //验证栏目代码
    namespace app\admin\validate;

use think\Validate;

class Cate extends Validate
{
    protected $rule = [
        'catename'  =>  'require|max:25|unique:cate',
    ];

    protected $message  =   [
        'catename.require' => '栏目名称不能为空! ', 
        'catename.unique' => '栏目名称不能重复! ', 
        'catename.max' => '栏目名称不能大于25位! ', 
    ];

    ?>

(2)文章表的创建和页面的搭建

百度编辑器插件的安装方法:https://www.cnblogs.com/haima/p/9697639.html
与栏目的操作类似

2.文件添加附件上传

在Article控制器下添加如下代码完成单个文件上传

<?php
   if($_FILES['pic']['tmp_name']){
            $file= request()->file('pic');
            // 移动到框架应用根目录/public/uploads/ 目录下
            // echo ROOT_PATH ; die;
            $info = $file->move(ROOT_PATH . 'public' . DS . '/static/uploads');
            if($info){
                // 成功上传后 获取上传信息
                $data['pic']='/static/uploads/'.date('Ymd').'/'.$info->getFilename();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
               // echo $info->getFilename(); 
            }else{
                // 上传失败获取错误信息
                return $this->error($file->getError());
            }    
        }
?>

3.详解验证场景及文章添加问题修复

<?php
 //场景验证  
 namespace app\admin\validate;

  use think\Validate;

class Article extends Validate
{
    protected $rule = [
        'title'  =>  'require|max:100|unique:article',
        'keywords'  =>  'require',
        'cateid'  =>  'require',
    ];

    protected $message  =   [
      'title.require' => '标题不能为空! ', 
      'title.unique' => '标题名称不能重复! ', 
      'title.max' => '标题不能大于100位! ',
      'keywords.require' => '关键字不能为空! ', 
      'cateid.require' => '所属栏目不能为空! ', 
    ];
}

?>

4.文章列表显示

<?php
                 <div class="result-content">
                <table class="result-tab" width="100%">
                    <tr>
                        <th  width="3%">ID</th>                         
                        <th>文章标题</th>
                        <th>缩略图</th>
                        <th>点击量</th>
                        <th>所属栏目</th>
                        <th>发布时间</th>
                        <th>操作</th>                                                                     
                    </tr>
                    {volist name="artres" id='vo'}
                    <tr>
                        <td>{$vo.id}</td>
                        <td>{$vo.title}</td>
                        <td>
                            {if condition="$vo['pic'] neq '' "}
                            <img height='50' src='__PUBLIC__{$vo.pic}'>
                            {else /}
                            暂无缩略图
                            {/if}
                        </td>
                        <td>{$vo.click}</td>
                        <td>{$vo.catename}</td>
                        <td>{$vo.time|date='Y-m-d',###}</td>
                        <td>
                            <a class="link-update" href='{:url('edit',array("id"=>$vo['id']))}'>修改</a>
                            <a class="link-del" onclick="return confirm('你确定要删除该栏目吗?');" href='{:url('del',array("id"=>$vo['id']))}'>删除</a>
                        </td>
                    </tr>
                    {/volist}
                </table>
                <div class='list-page'>{$artres->render()}</div>
            </div>
?>

在lst.php中添加分页

<?php
    style type='text/css'>
ul.pagination li{
    margin:0px auto;
}

</style>
?>

5.文章修改完成

<?php
                          <tr>
                            <th>描述:</th>
                            <td><textarea name="desc" class="common-textarea" id="desc" cols="20" style="width: 50%;" rows="5">
                                {$arts.desc}
                            </textarea></td>
                              
                        </tr>
                        </tr>
                        <tr>
                            <th>所属栏目:</th>
                            <td>
                                <select name='cateid'>
                                    {volist name='cateres' id='vo'}
                                    <option {if condition="$vo['id'] eq $arts['cateid']"}selected{/if} value='{$vo.id}'>{$vo.catename}</option>
                                    {/volist} 
                                </select>  
                            </td>
                        </tr>
                        </tr>
                        <tr>
                            <th>缩略图:</th>
                            <td><input type='file' name='pic' />
                                {if condition="$arts['pic']  eq '' "}
                                暂无缩略图
                                {else /}
                              
                                <img height='50' src='__PUBLIC__{$arts.pic}'>                                 
                                {/if}
                            </td>
                        </tr>
                        <tr>
                            <th>内容</th>
                            <td><textarea name="content" class="common-textarea" id='content'  cols="30"   style="width: 98%;" rows="10">
                                <?php echo htmlspecialchars_decode($arts['content']);?>
                          
                            </textarea></td>
                        </tr>
?>

添加隐藏域

<?php
     <input type='hidden' name='id' value='{$arts.id}'>
?>

删除操作控制器

<?php
    public function del(){
    $id=input('id');
    if(db('article')->delete($id)){
         return $this->success('删除文章成功!','lst');
     }else{
      return $this->error('删除文章失败!');
   }
}
?>
2020-10-09_194249.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。