TP5.1实现简易留言板之一


基本功能,点赞,留言,评论。

最简单的留言板,需要的数据表 用户表,评论表,留言内容表。

用户表:

id name pwd username uerimg (ID 用户账号 密码 昵称 头像)

留言表:

id username time body dianzan taoyan (ID 用户昵称 发布留言的时间 留言内容 点赞数 反对数)

评论表:

id uid toid pinglunneirong pltime text_id (ID 被评论留言发布者的账号 对留言评论的用户账号 评论的时间 被评论文章的ID)

留言的功能非常好实现,就是数据库基本的增删改查。

主要是点赞和反对还有查看留言对于我这个前端渣渣来说,实现有点难度,为了这个抓紧学了jQuery和ajax。

还有就是对留言内容评论的表,要想好怎么构建。具体的像知乎,或者朋友圈的那种回复模式,等看大佬们怎么写的,学习学习。

第一个版本不对用户重复点赞做限制,要是做限制,我个人会在留言表中加一个toid负责记录对文章点赞的人的账号,下次这个人再对已经点赞过的文章重复点赞时,用ajax接收返回过来的警告 用alert提示用户,并且不把这次的数据传到数据库中。反对也是这样。至于怎么样点赞按钮之后,取消点赞,就把不显示alert,然后直接传到数据库减一。具体看第二版本。

还有就是显示留言的问题,其实这个你做一个分页就可以很好地解决了,tp5也提供了这个现成的东西直接拿来用,可惜我是在做完之后,才渐渐熟悉了tp5的杂项和扩展功能怎么样用。所以这些完善都在第二版本了。

还有一点就是前端的动态显示,这就要用JS了。PHP我还没学好,苦逼抓紧学了jQuery。不得不说,学了之后,好多东西都可以交给前端做,不用后端了,省事。

主要的代码(都写在一个控制器里了)

不知道为啥,插入代码之后,就不能再后面写东西了。这就比较尴尬。。。。。 看另一篇里面有jQuery

<?php
namespace app\index\controller;

use think\Controller;
use think\facade\Request;
use think\Db;
use think\facade\Session;


class Index extends Controller
{
  public function index()
  {
    if (Request::instance()->isPost()) {
      $name = Request::instance()->post('name');
      $pwd  = Request::instance()->post('pwd');

      $res = Db::table('userlog')->where('name', $name)->find();
      if ($res) {
        if ($res['pwd'] == $pwd && $res['name'] == $name) {
          //开启session
          Session::set('name', $name);
          Session::set('username', $res['username']);
          Session::set('userimg',$res['userimg']);
          return $this->success('登录成功', 'http://liuyanban.cn/public/index.php/index/index/body', -1, 50);
        } else {
          echo  '传入的用户名' . $name . '查到的用户名' . $res['name'] . '查到的密码' . $res['pwd'] . '传入的密码' . $pwd;
          return $this->error('用户或密码错误不存在', "http://liuyanban.cn/public/index.php/index", -1, 50);
        }
      } else {
        return $this->error('用户不存在', "http://liuyanban.cn/public/index.php/index");
      }
    }
    return $this->fetch();
  }

  public function register()
  {
    $rout = __DIR__;
    if (Request::instance()->isPost()) {
      $name = Request::instance()->post('name');
      $pwd  = Request::instance()->post('pwd');
      $username = Request::instance()->post('username');
      $userimg = Request::file('txfile');
      if(empty($userimg)){
        $this->error('!!!',"www.baidu.com",-1,50);
      }
      $rout = substr($rout,0,24);
      $rout = $rout."public\img\\";
      $info = $userimg->move($rout);
      echo "<br>";
      $str_info  = $info->getSaveName();
      $str_info  = $rout.$str_info;
      $data = ['name' => $name, 'pwd' => $pwd, 'username' => $username,'userimg'=>$str_info];
      $mark = Db::table('userlog')->insert($data);
      if ($mark) {
        return $this->success('注册成功,请登录', 'http://liuyanban.cn/public/index.php/index');
      }
    }
    return $this->fetch();
  }

  public function body()
  {
    $username = Session::get('username');
    $name = Session::get('name');
    $this->assign('username', $username);
    $res = Db::table('userlog')->where('name',$name)->find();
    $userimg = $res['userimg'];
    $userimg = substr($userimg,-57,);
    $str = 'http://liuyanban.cn'.$userimg;
    $this->assign('userimg',$str);
    $res = Db::table('liuyanneirong')->order('id', 'desc')->select();
    $this->assign('list', $res);

    //$value_1 = huifu();

    return $this->fetch();
  }

  public function edit()
  {
    $username = Session::get('username');
    if (Request::instance()->isPost()) {
      $text = Request::instance()->post('text_body');

      $data = ['username' => $username, 'body' => $text];
      $check  = Db::table('liuyanneirong')->insert($data);
      if ($check) {
        return $this->success('发布成功', 'http://liuyanban.cn/public/index.php/index/index/body');
      } else {
        return $this->error('发布失败', 'http://liuyanban.cn/public/index.php/index/index/edit');
      }
    }
    return $this->fetch();
  }

  public function dianzan()
  {
    if (Request::instance()->isPost()) {
      $id = Request::instance()->post('id');
      $res = Db::table('liuyanneirong')->where('id', $id)->find();
      $res['dianzan'] = $res['dianzan'] + 1;
      Db::table('liuyanneirong')->where('id', $id)->update(['dianzan' => $res['dianzan']]);
    }
  }

  public function taoyan()
  {
    if (Request::instance()->isPost()) {
      $id = Request::instance()->post('id');
      $res = Db::table('liuyanneirong')->where('id', $id)->find();
      $res['taoyan'] = $res['taoyan'] + 1;
      Db::table('liuyanneirong')->where('id', $id)->update(['taoyan' => $res['taoyan']]);
    }
  }

  public function pinglun()
  {
    $uid = Session::get('username'); //收集评论用户的name
    if (Request::instance()->isPost()) {
      //评论功能概述
      // 1. 建立评论表 由id 当前用户id 被评论用户id  以及评论内容  组成
      // 2. 查表 查 liuyanneirong 表,
      // 3. 往 pinglun 表里写入数据 
      $id = Request::instance()->post('id');
      $text_body = Request::instance()->post('text-body');
      $res = Db::table('liuyanneirong')->where('id', $id)->find();
      //查到被评论用户name
      $toid = $res['username'];
      $textid = $res['id'];
      $data = ['uid' => $uid, 'toid' => $toid, 'pinglunneirong' => $text_body, 'text_id' => $textid];
      //存入评论
      Db::table('pinglun')->insert($data);
    }
  }

  public function huifu()
  {
    //查看评论回复
      $text_id = Request::post('id');
      $respl = Db::table('pinglun')->where('text_id', $text_id)->select();
      //$respl = Db::table('pinglun')->where('text_id', $text_id)->find();
      if(empty($respl))
      {
         return  json($respl);
      }else{
         return  json($respl);
      }
  }
}

点击并拖拽以移动


文章作者: blue—sky
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 blue—sky !
 上一篇
TP5.1实现简易留言板之二 TP5.1实现简易留言板之二
比较尴尬。。。反正是自己的学习记录。。。无所谓哪里了。 怎么获取留言内容以及精确评论呢?我是用{volist}{/volist}标签做了一个循环然后 <button class="good" id="{$vo.id}" type="b
2020-05-22
下一篇 
实现类似看云的最基本功能(类似markdow) 实现类似看云的最基本功能(类似markdow)
依旧是搬运自己的csdn博客要求:实现类似看云的最基本功能。 语言:PHP ,框架Thinkphp 要点:如何实现同一页面展示不同内容、实现静态html文件自动更新、PHP如何遍历所有文件以及文件夹、读取文件内容Thinkphp的标签的使用
2020-05-22
  目录