ZendFramework implements message books with basic functions (with demo source code download)

Source: Internet
Author: User
This example describes how to use ZendFramework to Implement Message books with basic functions. Share it with you for your reference. The details are as follows: a message book... the basic function is. 1. post a message. 2. reply to message. 3. manage messages (modify, delete, and other operations ). I only wrote basic operations here, such as adding a message verification code. I have no page beautification or anything.

This example describes how to use Zend Framework to implement message books with basic functions. Share it with you for your reference. The details are as follows: a message book... the basic function is. 1. post a message. 2. reply to message. 3. manage messages (modify, delete, and other operations ). I only wrote basic operations here, such as adding a message verification code. I have no page beautification or anything.

This example describes how to use Zend Framework to implement message books with basic functions. We will share this with you for your reference. The details are as follows:

A message book... has the basic functions of .. 1. Posting messages. 2. Replying to messages. 3. Managing messages (modifying, deleting, and other operations ).

I only wrote basic operations here, such as adding a message verification code and page beautification.
I didn't do it. I just gave you an idea. We have to learn many things on our own.

The other is that AJAX is used for my messages. it's just your post. the data is displayed on the page .. but you need to understand Jquery's AJAX usage .. I believe most people will use this JS class library.

It doesn't matter if you don't understand it .. you can change it to not AJAX .. you just need to convert the submission action of the from message to an action under our control .. I believe this is not a problem. okay .. start work:

Our directory structure is the same as before, and remains unchanged. We need to change the directory structure. Don't worry. I will teach you how to do it:

First, set up our template page (View )..

According to the directory of the previous tutorial. application/views/scripts, there are some template pages under the directory. For example, (index. phtml, edit. phtml). We will delete them... now add a message folder.

Add (edit. phtml, index. phtml, message. phtml) Three template files. after completion. add (header. phtml, footer. phtml) Two template files.

Because these two files will be reused later .. so put them directly under application/views/scripts .. the template has been created. add an HTML file. JS. IMAGE. I put them all under the public folder in the root directory of the website. you can refer to my source code .. if it's a bit messy .. please refer to this tutorial according to the source code .. (^_^ sorry, I can only express it like this. I don't know how to write it so that you can better understand it. thank you for your understanding !)

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

1. we add the following fields to the original table: pid (whether the flag is a reply, and 0 indicates a message. 0 indicates a reply, author indicates a message sender, headimg indicates a message sender's profile, email indicates a message sender's email, and ip indicates a message sender's ip address ),
Show (whether the message is displayed. this can be used for production management. this tutorial is not used here .), addtime (Message time), updatetime (Message modification time ). for field type settings, see the source code SQL file.

2. We have a Message. php In the application/models/directory. We will first write the Model of our Message book. It mainly operates on the data layer of the Message book. I have added the following methods:
GetAllMessage, getAllReMessage, getMessageByID, updateMessageByID, and delMessageByID)

The specific program is as follows (the program is also Annotated ):

Class Message extends Zend_Db_Table {protected $ _ name = "message"; protected $ _ primary = 'id';/** get all messages */public function getAllMessage () {$ messageArray = $ this-> fetchAll ("message. pid = 0 "," message. id DESC ")-> toArray (); return $ messageArray;}/** get all response message data */public function getAllReMessage () {$ ReArray = $ this-> fetchAll ("message. pid! = 0 "," message. id DESC ")-> toArray (); return $ ReArray;}/** retrieve 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, we have the control layer (Controller ). open application/controllers/IndexController. php controller .. delete the unwanted items. I added the following

A message method (also called Action ). however, other actions have been modified .. please participate in the source code for analysis. here I only paste the newly added messageAction method (the Code has annotations. 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'); $ mess Ageid = $ 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) {// obtain the IP address .. here we simply take the IP address $ IP =$ _ SERVER ["REMOTE_ADDR"]; $ data = array ('title' => $ title, 'author' => $ username, 'pid '=> $ messageid, 'uploadmg' => $ headimg, 'email '=> $ email, 'show' => '1 ', 'content' => $ content, 'IP' => $ ip, 'addtime' => time (), 'updatetime' => time ()); $ message-> insert ($ dat A); $ db-> lastInsertId (); unset ($ data); // get all the messages getAllMessage, getAllReMessage // two methods in Model (Message. $ this-> view-> messages = $ message-> getAllMessage (); // retrieve all response data $ this-> view-> arrReviews = $ message-> getAllReMessage (); $ this-> view-> flag = '0 '; $ this-> view-> message = 'Your message is published successfully! '; Echo $ this-> view-> render ('message/message. phtml');} else {$ this-> view-> flag = '5'; $ this-> view-> message = 'sorry! The email address you entered is incorrect! '; Echo $ this-> view-> render ('message/message. phtml') ;}} elseif ($ username = '') {$ this-> view-> flag = '1 '; $ this-> view-> message = 'sorry! Your name cannot be blank! '; Echo $ this-> view-> render ('message/message. phtml');} elseif ($ messageid = '') {$ this-> view-> flag = '2'; $ this-> view-> message = 'sorry! Enter the reply message number! '; Echo $ this-> view-> render ('message/message. phtml');} elseif ($ content = '') {$ this-> view-> flag = '3'; $ this-> view-> message = 'Sorry! The message content you entered cannot be blank! '; Echo $ this-> view-> render ('message/message. phtml') ;}} else {echo $ this-> view-> render ('message/index. phtml ');}}

The verification code and paging function are not provided. The subsequent tutorials will be further explained.

Conclusion: the compilation of a message book is completed here. of course, this is a simple feature .. or that sentence. I just write what I will to you .. it's just an idea .. I only want .. so the write is good or not good .. please weigh yourself

Click here to download the complete instance code.

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.