thinkPHP中的文章详情页实现“上一篇下一篇”功能经验分享

前段时间在公司中接触到了用thinkPHP搭建的项目,其中涉及到了文章详情页上一篇下一篇翻页的功能实现效果。

因为刚接触这套框架和PHP,所以整理一下实现该功能的经验方法。

如果有不到位的地方,欢迎指正!先看效果图:

 

 其中涉及到了第一篇之前没有文章和最后一篇没有文章的问题。

<---------------------------------------------------------------------------------->

一.首先在Controller中获取从列表页传来的文章ID

二.设置查询到上下篇的条件,通过定义关联数组传入到where查询语句中去

三.在view层绑定数据,通过if else语句判断Controller传来的$pre,$next数组是否为空,如果为空则显示上下篇无文章

这里用的是empty()方法判断Array是否为空,也有其他方法。

四.给上下篇无文章的情况设置灰色无法点击的样式。

 

 

刚接触PHP不久,如果说的不对的地方,欢迎交流指正!

 

转载于:https://www.cnblogs.com/lwj-blog/p/11205789.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的用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实现文章管理功能的示例代码。当然,实际开发还需要考虑数据验证、权限控制、异常处理等问题,这里只是提供一个基础的框架。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值