PHP留言本,非常适合新手实战操作!

闲来没事,应学习群里面一些人的要求,花了几个小时写了一个简单的PHP留言本,很适合PHP新手学习,当然老手可以略过~~~~

架构:PHP+MYSQL+Layui+smarty,实现简单的MVC构架,一个页面完成留言本的增、删、改、回复等功能。

文件目录结构如下图:

 

实现代码就比较简单了,总共100多行代码实现。

class IndexController extends Site {

    private $model;
    private $DB;

    public function __construct(){
        parent::__construct();
        $this->model=new Model();
        $this->DB='www_message';
    }

    /**
     * 首页列表
     */
    public function index(){
        $page_size=3;//页显示数,根据自己需要调整
        $pageCurrent=!empty($_GET["p"])?$_GET['p']:'1';
        $currentNum=($pageCurrent-1)*$page_size;
        $sql="select * from `".$this->DB."` ORDER BY id desc";
        $query=$sql." limit $currentNum,$page_size";
        $reccount=mysqli_num_rows($this->model->query($sql));

        $list=$this->model->query($query);
        $page=Pager('',$reccount,$page_size,$pageCurrent,10);

        $this->assign('list',$list);
        $this->assign('pager',$page);
        $this->display('index.php');
    }

    //删除留言操作
    public function delete(){
        $id=$_GET['id'];
        $where['id']=$id;
        $result=$this->model->delete($this->DB,$where);
        if($result==true){
            exit(json_encode(array('status'=>true,'info'=>'删除成功')));
        }else{
            exit(json_encode(array('status'=>false,'info'=>'删除失败')));
        }
    }

    /**
     * 添加留言操作
     */
    public function add(){
        $postData=$_POST['info'];
        $postData['create_time']=time();
        $postData['uip']=get_client_ip();
        $res=$this->model->inserttable($this->DB,$postData);
        if($res){
            exit(json_encode(array('status'=>true,'info'=>'留言成功')));
        }else{
            exit(json_encode(array('status'=>false,'info'=>'留言失败')));
        }
    }

    /**
     * 回复留言
     */
    public function edit(){
        if($_SERVER['REQUEST_METHOD']=='POST'){
            $postData=$_POST['info'];
            $where['id']=$postData['id'];
            unset($postData['id']);
            $res=$this->model->updatetable($this->DB,$postData,$where);
            if($res){
                exit(json_encode(array('status'=>true,'info'=>'留言修改成功','isclose'=>true)));
            }else{
                exit(json_encode(array('status'=>false,'info'=>'留言修改失败')));
            }
        }else{
            $msgid=$_GET['id'];
            $msgData=$this->model->getone('select `id`,`title`,`content` from `'.$this->DB.'` where id='.$msgid);
            if(empty($msgData)){
                exit('您查看的留言不存在或被删除!');
            }else{
                $this->assign('msgdata',$msgData);
                $this->display('edit.php');
            }
        }
    }

    /**
     * 回复留言
     */
    public function reply(){
        if($_SERVER['REQUEST_METHOD']=='POST'){
            $postData=$_POST['info'];
            $postData['reply_time']=time();
            $where['id']=$postData['id'];
            unset($postData['id']);
            $res=$this->model->updatetable($this->DB,$postData,$where);
            if($res){
                exit(json_encode(array('status'=>true,'info'=>'回复留言成功','isclose'=>true)));
            }else{
                exit(json_encode(array('status'=>false,'info'=>'回复留言失败')));
            }
        }else{
            $msgid=$_GET['id'];
            $msgData=$this->model->getone('select * from `'.$this->DB.'` where id='.$msgid);
            if(empty($msgData)){
                exit('您查看的留言不存在或被删除!');
            }else{
                $this->assign('msgdata',$msgData);
                $this->display('reply.php');
            }
        }
    }
}

以下是部分效果展示:

 

页面功能比较简单,暂未添加管理员管理留言功能,需要同僚的可以加群共同学习!

演示地址:http://www.phpteach.com/show/guestbook/

源码可以到此处下载: https://download.csdn.net/download/helly826/11372889

  • 16
    点赞
  • 132
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

helly826

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值