Zend framework to achieve a basic function of the message (with demo source download) _php instance

Source: Internet
Author: User
Tags zend zend framework

This article describes the Zend Framework to achieve the basic functions of the message book. Share to everyone for your reference, specific as follows:

A message book ... The basic function is. 1. Leave a message. 2. Reply to the message. 3. Manage message (modify, delete, etc.).

I just write the basic operation here, such as add message Verification code. The beautification of the page I have
Didn't do it. I'm just giving you a thought. A lot of things depend on us to learn.

The other is my message with Ajax. That's when you publish. The data will be displayed on the page. But you have to understand the Ajax usage of jquery. I believe most people will be this JS class library.

It doesn't matter if you don't understand. You can change it to a not Ajax. Just convert the submit action from the message to a move in our control. Believe that this is not a problem. All right. Start work:

Our directory structure is the same as before, roughly unchanged. Here are the changes. Don't worry, everybody. I'll teach you how to do it:

First: Set up our template page (View) first.

Follow the directory in the previous tutorial. There are some template pages in the Application/views/scripts directory. such as (index.phtml,edit.phtml). We delete them. Now add a message folder.

Add (edit.phtml,index.phtml,message.phtml) three template files in the message. When you're done. We add (header.phtml, in the application/views/scripts/catalogue). footer.phtml) two template files.

Because these two files will later be used to ... So put them directly under the application/views/scripts/. All right, the template is set up. Now it's time to join a HTML.JS.IMAGE. I put them all in the site root public folder. You can correspond to my source to see. If it's a little messy. Please look at this tutorial according to the source code. (^_^ Sorry, I can only say so.) I don't know how to write to make you understand better. Please be considerate!

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

1. We add the following fields in the original table: PID (flag is a reply, 0 for the message. For 0 is for the reply), author (message), Headimg (guestbook), email (message person email), IP (IP address of the message),
Show (whether the message is displayed. This should be managed in the health station. This tutorial is not used here.), Addtime (Message time), UpdateTime (Message modification time). setting of field type please see source SQL file.

2. We have a message.php in the application/models/catalogue. Let's write the model of our message book. The main is the message of this data layer operation. I added the following methods:
Getallmessage (take all the message), Getallremessage (take all the reply message data), Getmessagebyid (according to the ID to take the message data), Updatemessagebyid (change message), Delmessagebyid (Delete message)

The specific procedures are as follows (the program also has annotations):

 class message extends Zend_db_table {protected = ' message ';
 Protected $_primary = ' id '; * * Take all messages/Public function getallmessage () {$messageArray = $this->fetchall ("Message.pid=0", "Message.id DES
 C ")->toarray ();
 return $messageArray; * * * Fetch ALL reply message data/Public function Getallremessage () {$ReArray = $this->fetchall ("message.pid!=0", "message.i
 D DESC ")->toarray ();
 return $ReArray;
 * * * * message data based on ID/public function Getmessagebyid ($id) {$messageone = $this->fetchall (' id= '. $id)->toarray ();
 return $messageone;
 * * * Modify message/Public function Updatemessagebyid ($array, $id) {$db = $this->getadapter ();
 $where = $db->quoteinto (' id =? ', $id);
 $this->update ($array, $where);
   * * * Delete message/Public function Delmessagebyid ($id) {$where = ' id = '. $id;
   $this->delete ($where);
 return true; }
}

Third: Complete the above two items. Finally, on our control layer (Controller). Open application/controllers/indexcontroller.php this controller. Remove the original items that you didn't want. I added the following on top.

A message method (also called Action Action). But the rest of the action is changed. Please participate in the source code to carry out the analysis. Here I'll just post my new messageaction this method (with annotations on the code). Please check it 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 address ...
 Here simply take IP $IP =$_server ["REMOTE_ADDR"]; $data =array (' title ' => $title, ' author ' => $username, ' pid ' => $mEssageid, ' headimg ' => $headimg, ' email ' => $email, ' Show ' => ' 1 ', ' content ' => $content, ' IP ' => $IP, ' Addtim
 E ' =>time (), ' UpdateTime ' =>time ());
 $message->insert ($data);
 $db->lastinsertid ();
  Unset ($data); Take all messages Getallmessage,getallremessage//two methods defined in model (message.php) $this->view->messages= $message->
 Getallmessage ();
 Take all the reply data $this->view->arrreviews= $message->getallremessage ();
 $this->view->flag= ' 0 ';
 $this->view->message= ' Your message was published successfully! '
 echo $this->view->render (' message/message.phtml ');
 else {$this->view->flag= ' 5 ';
 $this->view->message= ' Sorry! You fill in the email address incorrectly! ';
 echo $this->view->render (' message/message.phtml ');
 }}elseif ($username = = ") {$this->view->flag= ' 1 ';
  $this->view->message= ' Sorry! Your name cannot be empty! ';
 echo $this->view->render (' message/message.phtml ');
 }elseif ($messageid = = ") {$this->view->flag= ' 2 ';
  $this->view->message= ' Sorry! Reply message number cannot be empty! '; Echo $this-≫view->render (' message/message.phtml ');
 }elseif ($content = = ") {$this->view->flag= ' 3 ';
  $this->view->message= ' Sorry! The message you fill out cannot be empty! ';
  echo $this->view->render (' message/message.phtml ');
  }else{echo $this->view->render (' message/index.phtml ');

 }
}

Just no verification code and paging functionality. A further tutorial will be available in the following article.

Summary: Here is the completion of a message book writing. Of course, it's a simple function. I'm just going to write it to everyone. Just a thought. That's all I can do. So write good and bad. Please make a balance on your own

Full instance code click here to download the site.

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.