简单MVC架构的PHP留言本,非常适合PHP新手实战学习~~~
主要框架:PHP+MYSQL+Smarty+Layui,实现简单的MVC架构,100多行代码实现留言本的增、删、改、回复等功能。非常适合PHP新手学习!
文件目录结构如下图:
实现代码就比较简单了,总共100多行代码实现。
1 class IndexController extends Site { 2 3 private $model; 4 private $DB; 5 6 public function __construct(){ 7 parent::__construct(); 8 $this->model=new Model(); 9 $this->DB='www_message'; 10 } 11 12 /** 13 * 首页列表 14 */ 15 public function index(){ 16 $page_size=3;//页显示数,根据自己需要调整 17 $pageCurrent=!empty($_GET["p"])?$_GET['p']:'1'; 18 $currentNum=($pageCurrent-1)*$page_size; 19 $sql="select * from `".$this->DB."` ORDER BY id desc"; 20 $query=$sql." limit $currentNum,$page_size"; 21 $reccount=mysqli_num_rows($this->model->query($sql)); 22 23 $list=$this->model->query($query); 24 $page=Pager('',$reccount,$page_size,$pageCurrent,10); 25 26 $this->assign('list',$list); 27 $this->assign('pager',$page); 28 $this->display('index.php'); 29 } 30 31 //删除留言操作 32 public function delete(){ 33 $id=$_GET['id']; 34 $where['id']=$id; 35 $result=$this->model->delete($this->DB,$where); 36 if($result==true){ 37 exit(json_encode(array('status'=>true,'info'=>'删除成功'))); 38 }else{ 39 exit(json_encode(array('status'=>false,'info'=>'删除失败'))); 40 } 41 } 42 43 /** 44 * 添加留言操作 45 */ 46 public function add(){ 47 $postData=$_POST['info']; 48 $postData['create_time']=time(); 49 $postData['uip']=get_client_ip(); 50 $res=$this->model->inserttable($this->DB,$postData); 51 if($res){ 52 exit(json_encode(array('status'=>true,'info'=>'留言成功'))); 53 }else{ 54 exit(json_encode(array('status'=>false,'info'=>'留言失败'))); 55 } 56 } 57 58 /** 59 * 回复留言 60 */ 61 public function edit(){ 62 if($_SERVER['REQUEST_METHOD']=='POST'){ 63 $postData=$_POST['info']; 64 $where['id']=$postData['id']; 65 unset($postData['id']); 66 $res=$this->model->updatetable($this->DB,$postData,$where); 67 if($res){ 68 exit(json_encode(array('status'=>true,'info'=>'留言修改成功','isclose'=>true))); 69 }else{ 70 exit(json_encode(array('status'=>false,'info'=>'留言修改失败'))); 71 } 72 }else{ 73 $msgid=$_GET['id']; 74 $msgData=$this->model->getone('select `id`,`title`,`content` from `'.$this->DB.'` where id='.$msgid); 75 if(empty($msgData)){ 76 exit('您查看的留言不存在或被删除!'); 77 }else{ 78 $this->assign('msgdata',$msgData); 79 $this->display('edit.php'); 80 } 81 } 82 } 83 84 /** 85 * 回复留言 86 */ 87 public function reply(){ 88 if($_SERVER['REQUEST_METHOD']=='POST'){ 89 $postData=$_POST['info']; 90 $postData['reply_time']=time(); 91 $where['id']=$postData['id']; 92 unset($postData['id']); 93 $res=$this->model->updatetable($this->DB,$postData,$where); 94 if($res){ 95 exit(json_encode(array('status'=>true,'info'=>'回复留言成功','isclose'=>true))); 96 }else{ 97 exit(json_encode(array('status'=>false,'info'=>'回复留言失败'))); 98 } 99 }else{ 100 $msgid=$_GET['id']; 101 $msgData=$this->model->getone('select * from `'.$this->DB.'` where id='.$msgid); 102 if(empty($msgData)){ 103 exit('您查看的留言不存在或被删除!'); 104 }else{ 105 $this->assign('msgdata',$msgData); 106 $this->display('reply.php'); 107 } 108 } 109 } 110 }
以下是部分效果展示:
页面功能比较简单,暂未添加管理员管理留言功能,需要同僚的可以加群共同学习!