文章编辑

一、功能列表:

  1. 添加文章;
  2. 根据id查询单篇文章;
  3. 根据id正序或逆序获取所有文章;
  4. 根据id删除单片文章;
  5. 根据id删除一个区段内的文章;
  6. 编辑文章;

二、实现:

1、添加文章:

前端文件,通过post方法获取所有数据,返回insert的方法中:

<body>
    <form method="post" action="insert">
        <div>
            <div><lable>标题</lable></div>
            <div><input type="text" name="title" /></div>
        </div>
        <div>
            <div><lable>作者</lable></div>
            <div><input type="text" name="author" /></div>
        </div>
        <div>
            <div><lable>内容</lable></div>
            <div><input type="text" name="words" /></div>
        </div>
        <div>
            <button formmethod="post" formaction="insert">提交</button>
        </div>
    </form>
</body>

后台文件:

public function insert()
{
    //获取前端数据
    $data = input('post.');

    $user = new UserModel($data);
    //录入数据库的表中
    $ret = $user->allowField(true)->save();
    if ($ret)
    {
        //success为tp5封装好的方法,用以弹出提示界面,第一个参数为文本信息内容,第二个参数为后续跳转页面
        $this->success('insert successfully','Articals/lists');
    }
    else
    {
        $this->error('insert failed');
    }
}

2、查询所有文章:

前端文件,编辑与添加方法需要传递相应的id值以供操作:

<table>
    <tr>
        <th width="10%">ID</th>
        <th width="10%">标题</th>
        <th width="10%">作者</th>
        <th width="10%">时间</th>
        <th width="50%">内容</th>
    </tr>
    {foreach $data as $value}
    <tr>
        <td style="text-align: center;">{$value.id}</td>
        <td style="text-align: center;">{$value.title}</td>
        <td style="text-align: center;">{$value.author}</td>
        <td style="text-align: center;">{$value.time}</td>
        <td style="text-align: center;">{$value.words}</td>
        <td>
            <div>
                <!--将id值传递给后台-->
                <a href="edit?id={$value.id}">编辑</a>
                <a href="delete?id={$value.id}">删除</a>
            </div>
        </td>
    </tr>
    {/foreach}
</table>

后台文件:

public function lists()
{
    //$order为主页中获取的排序方式。
    $order = input('get.order');
    //此方法返回一个二维数组
    if ($order == 'forwords')//正序
        $lists = Db::table('Articals')->order('id')->select();
    else//逆序
        $lists = Db::table('Articals')->order('id desc')->select();
    $this->assign('data',$lists);
    return $this->fetch();
}

3、删除文章

前端文件:

<form method="get" action="delete">
    <div>
        <input type="text" name="id" />
        <button formmethod="get" formaction="Articals/delete">删除</button>
    </div>
    <div>
        <input type="text" name="left" />
        <input type="text" name="right" />
        <button formmethod="get" formaction="Articals/multiDelete">区间删除</button>
    </div>
</form>

后台文件:

public function delete()
{
    $id = input('get.id');
    //真删除数据
    //软删除数据不添加true,即数据不在前台显示,但仍在后台保存
    $judge = UserModel::destroy($id,true);
    if ($judge)
        $this->success('delete successfully','Articals/lists');
    else
        $this->error('data not found');
}
public function multiDelete()
{
    $left = input('get.left');
    $right = input('get.right');

    $judge = Db::table('Articals')->where('id','between',[$left,$right])->delete();
    if ($judge)
        $this->success('delete successfully','Articals/lists');
    else
        $this->error('data not found');
}

4、编辑文章

前端文件:

<body>
    <form method="post" action="update">
        <!--$data由后台反馈而来,能够默认出现在文本框中-->
        <input type="hidden" name="id" value="{$data.id}" />
        <div>
            <div><lable>标题</lable></div>
            <div><input type="text" name="title" value="{$data.title}" /></div>
        </div>
        <div>
            <div><lable>作者</lable></div>
            <div><input type="text" name="author" value="{$data.author}" /></div>
        </div>
        </div>
        <div>
            <div><lable>内容</lable></div>
            <div><input type="text" name="words" value="{$data.words}" /></div>
        </div>
        <div>
            <button formmethod="post" formaction="update">提交</button>
        </div>
    </form>
</body>

后台文件:

public function edit()
{
    $id = input('get.id');
    $data = UserModel::get($id);
    $this -> assign('data',$data);//反馈给前端默认显示
    return $this->fetch();
}
public function update()
{
    $data = input('post.');
    $id = input('post.id');
    $user = new UserModel();
    $judge = $user->allowField(true)->save($data,['id' => $id]);
    if ($judge)
        $this->success('edit successfully','Articals/lists');
    else
        $this->error('edit failed');
}

CSUer