Home  >  Article  >  Backend Development  >  Zend Framework implements a guestbook with basic functions (with demo source code download), zenddemo_PHP tutorial

Zend Framework implements a guestbook with basic functions (with demo source code download), zenddemo_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:56:12866browse

Zend Framework implements a guestbook with basic functions (with demo source code download), zenddemo

This article describes the example of Zend Framework implementing a guestbook with basic functions. Share it with everyone for your reference, the details are as follows:

The basic functions of a guestbook are: 1. Post messages. 2. Reply to messages. 3. Manage messages (modify, delete, etc.).

I just wrote the basic operations here, such as adding a message verification code. I also did the page beautification
I haven’t done it yet. I’m just giving you an idea. We have to learn a lot of things on our own.

The other thing is that I use AJAX for my comments. As soon as you post it, the data will be displayed on the page. But everyone needs to understand the usage of Jquery's AJAX. I believe most people know this JS library.

It doesn’t matter if you don’t understand. You can change it to something other than AJAX. Just convert the submission action of FROM to post a message into an action in our control. I believe this is not a problem. Okay.. Let’s start working:

Our directory structure is roughly the same as before. There will be changes below. Don’t worry, I will teach you how to do it:

First: First create our template page (View)..

According to the directory in the previous tutorial, there are some template pages in the application/views/scripts directory, such as (index.phtml, edit.phtml). Let’s delete them. Now add a message folder.

Add three template files (edit.phtml, index.phtml, message.phtml) to the message. After completion, we add two (header.phtml, footer.phtml) to the application/views/scripts/ directory template files.

Because these two files will be reused in the future...so put them directly under application/views/scripts/...Okay, the template is created. Now it is time to add an HTML.JS.IMAGE. I put them These are placed in the public folder in the root directory of the website. You can take a look at my source code. If it is a bit messy, please read this tutorial based on the source code. (^_^ Sorry, I can only express it this way. .I don’t know how to write so that you can understand better. Please understand!)

Second: Next, we write our data layer program (Model).

1. We add the following fields to the original table: pid (marks whether it is a reply, 0 is a message. If it is 0, it is a reply), author (the person who left the message), headimg (the avatar of the person who left the message) ,email (email of the person who left the message), ip (IP address of the person who left the message),
show (Whether the message is displayed. This must be available in the station management. It is not used here in this tutorial.), addtime (message time), updatetime (message modification time). Please refer to the source code SQL file for the field type settings.

2. We have a Message.php in the application/models/ directory. We first write the Model of our guestbook. It mainly operates the guestbook data layer. I added the following methods:
getAllMessage (get all messages), getAllReMessage (get all reply message data), getMessageByID (get message data based on ID), updateMessageByID (modify message), delMessageByID (delete message)

The specific procedure is as follows (there are also notes on the procedure):

class Message extends Zend_Db_Table
{
 protected $_name ="message";
 protected $_primary = 'id';
 /* * 取到所有的留言 */
 public function getAllMessage(){
 $messageArray=$this->fetchAll("message.pid=0",
      "message.id DESC")->toArray();
 return $messageArray;
 }
 /* * 取到所有的回复留言数据 */
 public function getAllReMessage(){
 $ReArray=$this->fetchAll("message.pid!=0",
      "message.id DESC")->toArray();
 return $ReArray;
 }
 /* * 根据ID取留言数据 */
 public function getMessageByID($id){
 $messageone=$this->fetchAll('id='.$id)->toArray();
 return $messageone;
 }
 /* * 修改留言 */
 public function updateMessageByID($array,$id){
 $db = $this->getAdapter();
 $where=$db->quoteInto('id = ?', $id);
 $this->update($array, $where);
 }
 /* * 删除留言 */
 public function delMessageByID($id){
 $where = 'id = ' . $id;
   $this->delete($where);
   return true;
 }
}

Third: Complete the above two items. Finally, our control layer (Controller). Open the application/controllers/IndexController.php controller... and delete the original unnecessary things. I added above Below

A message method (also called Action). However, other Actions have been changed. Please participate in the source code for analysis. Here I only post my newly added messageAction method (there are comments on the code. Please Check it out by yourself. Thank you):

public function messageAction()
{
 if($this->_request->isPost()){
 Zend_Loader::loadClass('Zend_Filter_StripTags');
 $filter=new Zend_Filter_StripTags();
 $username=$filter->filter($this->_request->getPost('username'));
 $email=$filter->filter($this->_request->getPost('email'));
 $content=$filter->filter($this->_request->getPost('content'));
 $title=$filter->filter($this->_request->getPost('title'));
 $messageid=$filter->filter($this->_request->getPost('messageid'));
 $headimg=$filter->filter($this->_request->getPost('headimg'));
 $message=new Message();
 $db=$message->getAdapter();
 if($username!=''&&$email!=''&&$messageid!=''&&$content!=''){
 require_once 'Zend/Validate/EmailAddress.php';
 $validator = new Zend_Validate_EmailAddress();
 if ($validator->isValid($email)) {
 //取IP地址..这里只是简单取IP
 $IP=$_SERVER ["REMOTE_ADDR"];
 $data=array(
 'title'=>$title,
 'author'=>$username,
  'pid'=>$messageid,
 'headimg'=>$headimg,
 'email'=>$email,
 'show'=>'1',
 'content'=>$content,
  'ip'=>$IP,
 'addtime'=>time(),
 'updatetime'=>time()
 );
 $message->insert($data);
 $db->lastInsertId();
 unset($data);
  //取到所有留言getAllMessage,getAllReMessage
  //二个方法在Model(Message.php)里定义的
  $this->view->messages=$message->getAllMessage();
 //取到所有回复数据
 $this->view->arrReviews=$message->getAllReMessage();
 $this->view->flag='0';
 $this->view->message='您的留言发表成功!';
 echo $this->view->render('message/message.phtml');
 } else {
 $this->view->flag='5';
 $this->view->message='对不起!您填写有电子邮箱地址有误!';
 echo $this->view->render('message/message.phtml');
 }
  }elseif($username==''){
  $this->view->flag='1';
 $this->view->message='对不起!您的大名不能为空!';
  echo $this->view->render('message/message.phtml');
 }elseif($messageid==''){
  $this->view->flag='2';
 $this->view->message='对不起!回复留言编号不能为空!';
  echo $this->view->render('message/message.phtml');
 }elseif($content==''){
  $this->view->flag='3';
 $this->view->message='对不起!您填写的留言内容不能为空!';
  echo $this->view->render('message/message.phtml');
  }
  }else{
  echo $this->view->render('message/index.phtml');
  }
}

It just doesn’t have the verification code and paging function. There will be a tutorial to explain it further in the next article.

Summary: So far, I have completed writing a guestbook. Of course, the function is very simple... Again, I just write what I know to everyone... It's just a thought... I only know this. .So whether the writing is good or not...please weigh it yourself

Click here to download the complete example code from this website.

Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.

Articles you may be interested in:

  • Zend Framework Tutorial - Zend_Db_Table_Rowset Usage Example Analysis
  • Zend Framework Tutorial - Zend_Db_Table_Row Usage Example Analysis
  • Zend Framework Tutorial: Detailed explanation of Zend_Db_Table usage
  • Zend Framework tutorial: Zend_Form component to implement form submission and display error prompts
  • Zend Framework development introduction classic tutorial
  • Zend Framework Smarty extension implementation Method
  • Zend Framework framework routing mechanism code analysis
  • Zend Framework implements the method of storing session in memcache
  • Detailed explanation of Zend Framework paging class usage
  • Zend Framework implements multiple file upload function examples
  • Environment configuration for getting started with Zend Framework and the first Hello World example (with demo source code download)
  • Zend Framework tutorial method for connecting to the database and performing add/delete queries (Attached with demo source code download)
  • Zend Framework tutorial Zend_Db_Table table association example detailed explanation

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1113722.htmlTechArticleZend Framework implements a guestbook with basic functions (with demo source code download), zenddemo This article describes the implementation of Zend Framework with examples Guestbook with basic functions. Share it with everyone for your reference...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn