闲来没事,应学习群里面一些人的要求,花了几个小时写了一个简单的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