Zend Framework实现具有基本功能的留言本(附demo源码下载),zenddemo
Zend Framework实现具有基本功能的留言本(附demo源码下载),zenddemo
本文实例讲述了Zend Framework实现具有基本功能的留言本。分享给大家供大家参考,具体如下:
一个留言本...具有的基本功能就是.1.发表留言. 2.回复留言.3.管理留言(修改,删除等操作).
我这里只是写了基本的操作,比如加留言验证码.页面的美化什么的我都
没有做.我只是给大家一个思想..很多东西要靠我们自己去学了.
另一个就是我的留言用了AJAX.就是你一发表.数据就会在页面显示..不过大家要了解Jquery的AJAX的用法..我相信大部分人都会这个JS类库吧.
要是不懂也没关系..你们可以改成不是AJAX的..只要把发表留言的FROM的提交动作转换成我们控制里的一个动作..相信这个不是问题.好了..开始工作:
我们的目录结构和以前一样,大致不变..下面要有改动的..大家也不要急..我会教大家如何做:
第一:先建立好我们的模板页面(View)..
按照上篇教程的目录.application/views/scripts目录下有一些模板页.如(index.phtml,edit.phtml).我们删除它们..现在加一个message文件夹.
在message里加上(edit.phtml,index.phtml,message.phtml)三个模板文件.完成后.我们在application/views/scripts/目录下加上(header.phtml,footer.phtml)二个模板文件.
因为这二个文件以后会重用来..所以把他们直接放到application/views/scripts/下..好了模板建立好了.现在就是加入一个HTML.JS.IMAGE了.我把他们这些都放在网站根目录public文件夹下.大家可以对应我的源码来看一下..要是有点乱..请大家根据源码来看这教程..(^_^不好意思,我只能这样表达.我也不知道如何写才能让你们更了解.请大家体谅啊!)
第二:接下来,我们写我们的数据层程序(Model).
1.我们在原来的表中加上下面几个字段:pid(标志是否是回复,0为留言.为为0的是为回复) ,author(留言者),headimg(留言者头像),email(留言者电子邮件), ip(留言者IP地址),
show(留言是否显示.这个要在生台管理能用.这教程这里没有用到.), addtime(留言时间), updatetime(留言修改时间).字段类型的设置请大家看源码SQL文件.
2.我们在application/models/目录下有一个Message.php.我们先写好我们留言本的Model .主要是对留言本数据层的操作.我增加了下面几个方法:
getAllMessage(取到所有的留言) , getAllReMessage(取到所有的回复留言数据) , getMessageByID(根据ID取留言数据), updateMessageByID(修改留言), delMessageByID(删除留言)
具体程序如下(程序上面也有注解):
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; } }
第三:完成上面二项.最后就我们的控制层了(Controller).打开application/controllers/IndexController.php这个控制器..把原来的不要的东西给删除了.我在上面加上了下面
一个message方法(也叫动作Action).不过其它的Action都有改动..请大家参与源码来进行分析.这里我只贴也我新加入的messageAction这个方法(代码上都有注解.请自行查看.谢谢):
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'); } }
只是没有验证码以及分页功能..后面一篇会有教程进一步讲解.
总结:到这里就完成了一个留言本的编写.当然很简单的功能..还是那句话.我只是把我会的写给大家..只是一个思想..我也只会这些..所以写的好与不好..请大家自己进行权衡
完整实例代码点击此处本站下载。
更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
- Zend Framework框架教程之Zend_Db_Table_Rowset用法实例分析
- Zend Framework教程之Zend_Db_Table_Row用法实例分析
- Zend Framework教程之Zend_Db_Table用法详解
- Zend Framework教程之Zend_Form组件实现表单提交并显示错误提示的方法
- Zend Framework开发入门经典教程
- Zend Framework框架Smarty扩展实现方法
- Zend Framework框架路由机制代码分析
- Zend Framework实现将session存储在memcache中的方法
- Zend Framework分页类用法详解
- Zend Framework实现多文件上传功能实例
- Zend Framework入门之环境配置及第一个Hello World示例(附demo源码下载)
- Zend Framework教程之连接数据库并执行增删查的方法(附demo源码下载)
- Zend Framework教程之Zend_Db_Table表关联实例详解

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



.NET Framework 4 is required by developers and end users to run the latest versions of applications on Windows. However, while downloading and installing .NET Framework 4, many users complained that the installer stopped midway, displaying the following error message - " .NET Framework 4 has not been installed because Download failed with error code 0x800c0006 ". If you are also experiencing it while installing .NETFramework4 on your device then you are at the right place

Whenever your Windows 11 or Windows 10 PC has an upgrade or update issue, you will usually see an error code indicating the actual reason behind the failure. However, sometimes confusion can arise when an upgrade or update fails without an error code being displayed. With handy error codes, you know exactly where the problem is so you can try to fix it. But since no error code appears, it becomes challenging to identify the issue and resolve it. This will take up a lot of your time to simply find out the reason behind the error. In this case, you can try using a dedicated tool called SetupDiag provided by Microsoft that helps you easily identify the real reason behind the error.
![SCNotification has stopped working [5 steps to fix it]](https://img.php.cn/upload/article/000/887/227/168433050522031.png?x-oss-process=image/resize,m_fill,h_207,w_330)
As a Windows user, you are likely to encounter SCNotification has stopped working error every time you start your computer. SCNotification.exe is a Microsoft system notification file that crashes every time you start your PC due to permission errors and network failures. This error is also known by its problematic event name. So you might not see this as SCNotification having stopped working, but as bug clr20r3. In this article, we will explore all the steps you need to take to fix SCNotification has stopped working so that it doesn’t bother you again. What is SCNotification.e

Microsoft Windows users who have installed Microsoft.NET version 4.5.2, 4.6, or 4.6.1 must install a newer version of the Microsoft Framework if they want Microsoft to support the framework through future product updates. According to Microsoft, all three frameworks will cease support on April 26, 2022. After the support date ends, the product will not receive "security fixes or technical support." Most home devices are kept up to date through Windows updates. These devices already have newer versions of frameworks installed, such as .NET Framework 4.8. Devices that are not updating automatically may

It's been a week since we talked about the new safe mode bug affecting users who installed KB5012643 for Windows 11. This pesky issue didn't appear on the list of known issues Microsoft posted on launch day, thus catching everyone by surprise. Well, just when you thought things couldn't get any worse, Microsoft drops another bomb for users who have installed this cumulative update. Windows 11 Build 22000.652 causes more problems So the tech company is warning Windows 11 users that they may experience problems launching and using some .NET Framework 3.5 applications. Sound familiar? But please don't be surprised

PHP implementation framework: ZendFramework introductory tutorial ZendFramework is an open source website framework developed by PHP and is currently maintained by ZendTechnologies. ZendFramework adopts the MVC design pattern and provides a series of reusable code libraries to serve the implementation of Web2.0 applications and Web Serve. ZendFramework is very popular and respected by PHP developers and has a wide range of

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

According to news on December 9, Cooler Master recently demonstrated a mini chassis kit in cooperation with notebook modular solution provider Framework at a demonstration event at the Taipei Compute Show. The unique thing about this kit is that it can be compatible with and Install the motherboard from the framework notebook. Currently, this product has begun to be sold on the market, priced at 39 US dollars, which is equivalent to approximately 279 yuan at the current exchange rate. The model number of this chassis kit is named "frameWORKMAINBOARDCASE". In terms of design, it embodies the ultimate compactness and practicality, measuring only 297x133x15 mm. Its original design is to be able to seamlessly connect to framework notebooks
