ZendFramework implements message books with basic functions (with demo source code download) and zenddemo. ZendFramework implements message books with basic functions (with demo source code download). zenddemo this article describes the message books with basic functions implemented by ZendFramework. Share with you the message book (with demo source code download) and zenddemo with basic functions of Zend Framework.
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.