为什么总是删我的文章

CSDN管理员 ,为什么总是删我的文章,给个删除的理由好嘛?

如果再无故删除,我就只有换一个博客了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的用ThinkPHP实现文章管理功能的示例代码: 1. 创建数据库表 首先,我们需要创建一张名为`article`的数据表,用于存储文章的相关信息,包括文章ID、标题、内容、作者、发布时间等字段。 2. 创建模型 在ThinkPHP中,我们可以通过创建模型来操作数据库表。创建一个名为`ArticleModel`的模型,用于对文章数据进行增改查操作。 ```php namespace app\admin\model; use think\Model; class ArticleModel extends Model { protected $table = 'article'; protected $pk = 'id'; } ``` 在模型中,我们定义了数据表名和主键字段,这样就可以直接使用模型的方法对数据进行操作。 3. 创建控制器 创建一个名为`ArticleController`的控制器,用于处理文章管理功能的相关请求。 ```php namespace app\admin\controller; use think\Controller; use app\admin\model\ArticleModel; class ArticleController extends Controller { // 文章列表页 public function index() { $articleModel = new ArticleModel(); $articleList = $articleModel->order('id desc')->paginate(10); $this->assign('articleList', $articleList); return $this->fetch(); } // 添加文章页 public function add() { return $this->fetch(); } // 添加文章操作 public function doAdd() { $postData = $this->request->post(); $articleModel = new ArticleModel($postData); $result = $articleModel->allowField(true)->save(); if ($result) { $this->success('添加成功', url('Article/index')); } else { $this->error('添加失败'); } } // 修改文章页 public function edit() { $id = $this->request->param('id'); $articleModel = new ArticleModel(); $article = $articleModel->where('id', $id)->find(); $this->assign('article', $article); return $this->fetch(); } // 修改文章操作 public function doEdit() { $postData = $this->request->post(); $articleModel = new ArticleModel(); $article = $articleModel->where('id', $postData['id'])->find(); if (!$article) { $this->error('文章不存在'); } $article->allowField(true)->save($postData); $this->success('修改成功', url('Article/index')); } // 文章操作 public function doDelete() { $id = $this->request->param('id'); $articleModel = new ArticleModel(); $result = $articleModel->where('id', $id)->delete(); if ($result) { $this->success('除成功', url('Article/index')); } else { $this->error('除失败'); } } } ``` 在控制器中,我们定义了文章列表、添加、修改、除等操作的相关方法。其中,`index`方法用于显示文章列表,`add`方法用于显示添加文章的页面,`doAdd`方法用于处理添加文章的操作,`edit`方法用于显示修改文章的页面,`doEdit`方法用于处理修改文章的操作,`doDelete`方法用于处理文章的操作。 4. 创建视图文件 最后,我们需要创建对应的视图文件来显示文章管理页面和表单页面。 文章列表视图文件`index.html`: ```html <table> <thead> <tr> <th>ID</th> <th>标题</th> <th>作者</th> <th>发布时间</th> <th>操作</th> </tr> </thead> <tbody> <?php foreach ($articleList as $article): ?> <tr> <td><?php echo $article->id; ?></td> <td><?php echo $article->title; ?></td> <td><?php echo $article->author; ?></td> <td><?php echo $article->create_time; ?></td> <td> <a href="<?php echo url('Article/edit', ['id'=>$article->id]); ?>">编辑</a> <a href="<?php echo url('Article/doDelete', ['id'=>$article->id]); ?>">除</a> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php echo $articleList->render(); ?> <a href="<?php echo url('Article/add'); ?>">添加文章</a> ``` 添加文章视图文件`add.html`: ```html <form method="post" action="<?php echo url('Article/doAdd'); ?>"> <div> <label for="title">标题:</label> <input type="text" name="title" id="title"> </div> <div> <label for="author">作者:</label> <input type="text" name="author" id="author"> </div> <div> <label for="content">内容:</label> <textarea name="content" id="content"></textarea> </div> <div> <button type="submit">保存</button> </div> </form> ``` 修改文章视图文件`edit.html`: ```html <form method="post" action="<?php echo url('Article/doEdit'); ?>"> <input type="hidden" name="id" value="<?php echo $article->id; ?>"> <div> <label for="title">标题:</label> <input type="text" name="title" id="title" value="<?php echo $article->title; ?>"> </div> <div> <label for="author">作者:</label> <input type="text" name="author" id="author" value="<?php echo $article->author; ?>"> </div> <div> <label for="content">内容:</label> <textarea name="content" id="content"><?php echo $article->content; ?></textarea> </div> <div> <button type="submit">保存</button> </div> </form> ``` 以上就是一个简单的用ThinkPHP实现文章管理功能的示例代码。当然,实际开发中还需要考虑数据验证、权限控制、异常处理等问题,这里只是提供一个基础的框架。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值