Simple MVC framework of PHP Guestbook

Simple MVC framework of PHP Guestbook, PHP is very suitable for the novice to learn combat ~~~

The main framework: PHP + MYSQL + Smarty + Layui, simple MVC architecture, more than 100 lines of code to achieve Guestbook add, delete, change, reply and other functions. Very suitable for novices learn PHP!

File directory structure as shown below:

 

Implementation code is relatively simple, a total of more than 100 lines of code.

 
  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' => 'deletion failed' )));
 40          }
 41      }
 42 is  
43 is      / * *
 44       * add a message operation
 45       * / 
46 is      public  function the Add () {
 47          $ postData = $ _POST [ 'info' ];
 48          $ postData [ 'the create_time'] = Time ();
 49          $ postData [ 'UIP'] = get_client_ip ();
 50          $ RES = $ the this -> Model-> InsertTable ( $ the this -> DB, $ postData );
 51 is          IF ($ RES ) {
 52 is              Exit (json_encode ( Array ( 'Status' => to true ,' info '=>' message is successful ' )));
 53 is          } the else {
 54 is              Exit (json_encode ( Array (' Status' => to false , ' info '=>' message failed ' )));
 55          }
 56      }
 57 is  
58      / * *
 59       * reply message
 60       * / 
61 is      public  function Edit () {
 62 is          IF ( $ _SERVER [' REQUEST_METHOD '] ==' the 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' => to false , 'info' => 'Message modification failure' )));
 71 is              }
 72          } the else {
 73 is              $ msgid = $ _GET [ 'ID' ];
 74              $ msgData = $ the this -> Model-> getone ( 'SELECT id` `,` title`, content` from ``'. $ the this -> WHERE ID = DB.'` '. $ msgid );
 75              IF ( empty ( $ msgData )) {
 76                  Exit ( 'message that you are viewing does not exist or is deleted!' );
 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' => to false , 'info' => 'Reply Message failure' )));
 98              }
 99          } the else {
 100              $ msgid = $ _GET [ 'ID' ];
 101              $ msgData = $ the this -> Model -> getone ( 'SELECT * from `'. $ the this -> WHERE ID = DB.'` '. $ msgid ;)
 102              IF ( empty ( $ msgData )) {
 103                  Exit (' you're looking at the message does not exist or delete '! );
 104              } the else {
 105                  $ the this ->assign('msgdata',$msgData);
106                 $this->display('reply.php');
107             }
108         }
109     }
110 }

 

Here are some results show:

 

Page function is relatively simple, yet add administrators to manage message function, you need to add the group colleagues can learn together!

 

Demo Address: http://www.phpteach.com/show/guestbook/

Guess you like

Origin www.cnblogs.com/hellyliu/p/11199656.html