php留言板实战,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..net/download/helly826/11372889
總結
以上是生活随笔為你收集整理的php留言板实战,PHP留言本,非常适合新手实战操作!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php session_start()
- 下一篇: mysql和FTP结合,vsftp基于m