tp5.1简易留言板版本一(学习记录)

这篇博客记录了使用TP5.1框架创建一个简易留言板的过程,包括留言、评论和点赞功能。博主强调了数据库设计的重要性,如用户表、留言表和评论表的结构。对于前端交互,博主学习并应用了jQuery和ajax来处理动态更新和数据提交。虽然第一版未实现点赞的重复限制,但博主计划在后续版本中加入这一功能。同时,博主还提到分页和前端动态显示的技术细节,以及对PHP和jQuery的学习体验。

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

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

用户表:

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);
      }
  }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值