- 浏览: 13651099 次
- 性别:
- 来自: 洛杉矶
文章分类
- 全部博客 (1994)
- Php / Pear / Mysql / Node.js (378)
- Javascript /Jquery / Bootstrap / Web (435)
- Phone / IOS / Objective-C / Swift (137)
- Ubuntu / Mac / Github / Aptana / Nginx / Shell / Linux (335)
- Perl / Koha / Ruby / Markdown (8)
- Java / Jsp (12)
- Python 2 / Wxpython (25)
- Codeigniter / CakePHP (32)
- Div / Css / XML / HTML5 (179)
- WP / Joomla! / Magento / Shopify / Drupal / Moodle / Zimbra (275)
- Apache / VPN / Software (31)
- AS3.0/2.0 / Flex / Flash (45)
- Smarty (6)
- SEO (24)
- Google / Facebook / Pinterest / SNS (80)
- Tools (22)
最新评论
-
1455975567:
xuezhongyu01 写道wocan23 写道我想问下那个 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
xuezhongyu01:
wocan23 写道我想问下那个111.1是怎么得来的我也看不 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
18335864773:
试试 pageoffice 在线打开 PDF 文件吧. pag ...
jquery在线预览PDF文件,打开PDF文件 -
青春依旧:
opacity: 0.5; 个人喜欢这种方式!关于其他css特 ...
css透明度的设置 (兼容所有浏览器) -
July01:
推荐用StratoIO打印控件,浏览器和系统的兼容性都很好,而 ...
搞定网页打印自动分页问题
codeigniter: 显示提示信息 How do I display result or error messages in CodeIgniter app
- 博客分类:
- Codeigniter / CakePHP
Sometimes we need to display general messages or error messages to the user noticing about certain action, for example on success or failure.
Imagine you are submitting a form and then creating or updating the data in the database. You may be interested in noticing the user about the result for that action .
This is the way I use to display general messages (and error messages) in CodeIgniter application.
The result
This is how my message looks.
How to display the message?
Now, in order to display that information message , I do the following:
First, when submitting the form, after validating it, in the controller I call the model function that will save the entity (license in this case). I make sure that model function returns a result, and FALSE result in case of error (I usually return an array result on success with an ID element with the last inserted identifier if the result was an insert, or the just updated entity identifier if it was an update, but that is out of this article’s scope).
Controller
Imagine we are calling the following method in the controller:
$result = $this->license_model->create_or_update_license($agent_id, $state, $data);
Then, we need to see if that was an error or not. Based on that result, I will use CodeIgniter’s set_flashdata function in the Session library, as follows:
if ($result) { $this->session->set_flashdata( 'message', array( 'title' => 'License created', 'content' => 'License has been saved sucessfully', 'type' => 'message' )); redirect('agent/licenses'); } else { $this->session->set_flashdata( 'message', array( 'title' => 'License error', 'content' => 'License could not be saved', 'type' => 'error' )); redirect('agent/licenses'); }
Once the Flash message has been set, I redirect the user to the form or a list of results. That is needed in order to get the flash working (you cannot just load the view in this case… well, you can but this method will not work in such case). When comparing $result TRUE or FALSE, please notice the different value for type . I am using type=message for successful messages, and type=error for error mesages.
View
In the view, just above the form, I use this snippet.
<?= @flash_message() ?>
To keep it simple, I just added @ to avoid any error message, but probably if you are looking for a better coding practice and you may not do that.
A Helper
Then, I put this flash message function as a helper function, so it is available when calling from the view.
function flash_message() { // get flash message from CI instance $ci =& get_instance(); $flashmsg = $ci->session->flashdata('message'); $html = ''; if (is_array($flashmsg)) { $html = '<div id="flashmessage" class="'.$flashmsg[type].'"> <img style="float: right; cursor: pointer" id="closemessage" src="'.base_url().'images/cross.png" /> <strong>'.$flashmsg['title'].'</strong> <p>'.$flashmsg['content'].'</p> </div>'; } return $html; }
jQuery part
Finally, I added a simple effect to slide down and blink the message box , and adding the close functionality with a simple jQuery function. You can do something like this. Of course, you can modify it or just avoid using the jQuery effect if it is desired.
// first slide down and blink the message box $("#flashmessage").animate({top: "0px"}, 1000 ).show('fast').fadeIn(200).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
Some CSS styling
Finally, just add some CSS styling for #flashmessage, using message or error class names.
.message{ border:1px solid #CCCCCC; width:300px; border:1px solid #c93; background:#ffc; padding:5px; color: #333333; margin-bottom:10px; }
Enjoy it.
发表评论
-
CakePHP你必须知道的21条技巧
2012-10-19 06:25 1853原文链接:http://www.avatarfinancial ... -
cakephp 1.3 Views
2012-10-09 06:12 1415Section 1 Views 视图 一个vie ... -
cakephp 1.3 Models
2012-10-09 06:07 2478Section 1 What is a model? ... -
cakephp 1.3 Controller
2012-10-09 05:49 3308Controller 一个controller用于管理 ... -
cakephp 1.3 配置CakePHP
2012-10-09 05:31 4610Section 1 数据库配置 app/config/ ... -
CakePHP 2.x十分钟博客教程
2012-10-07 01:27 243771. CakePHP2十分钟博客教 ... -
Create an Admin panel with CodeIgniter
2010-05-23 02:15 4145Create an Admin panel with Code ... -
Codeigniter Grid 使用方法 (flexigrid)
2010-05-23 02:05 2769来源:http://codeigniter.com/forum ... -
CI集成 ckeditor 配置
2010-05-23 01:34 3727配置 ckeditor 1.上传 下载 ckedito ... -
codeigniter 辅助函数 - 敏感词过滤
2010-05-05 06:18 4550我们都知道有些敏感的词汇是不适合出现在互联网上的,特别是在有用 ... -
实现简单 codeigniter 缓存 (cache)
2010-04-30 23:47 5242代码 class Test extends Contr ... -
CKEditor Helper for CodeIgniter
2010-04-19 00:37 3947Using CKEditor as a plugin in y ... -
codeigniter 生成 excel
2010-04-19 00:33 3293Excel Plugin Th ... -
CakePHP 中文手册
2010-04-14 21:04 2316基本概念 Section1 简介 ... -
利用 Cache_Lite代替codeigniter中的cache功能
2010-01-29 06:15 5476codeigniter的功能纵然强大,也有不足之处。其cach ... -
CodeIgniter 操作PDF ( Generating PDF files using CodeIgniter )
2010-01-03 04:03 3574PDF files rock! Some of the p ... -
CodeIgniter 合作 Authorize.net
2009-12-30 00:25 1564function payment(){ // 略... ... -
CodeIgniter 合作paypal
2009-12-30 00:15 2322<?php class Paypal extend ... -
CodeIgniter 操作 CSV
2009-12-29 07:17 4591A Comma separated values (CSV) ... -
codeigniter 操作 Rss
2009-12-29 07:12 1932I wrote a codeigniter library t ...
相关推荐
1、CodeIgniter:php敏捷开发框架 2、CodeIgniter 1.7.1 用户指南 本书详细讲解了 CI 的一些主要特性。本书并不包含 CI 的所有内容和全部细节。CI 有一本出色的在线《用户指南》,它详细讲解了大多数的内容。它可以...
这篇文章是有关 CodeIgniter 的(以下简称 CI),CI 是一个达成以上目标的框架。 如果你只是要达成一个最终的结果,而把中间所有的编码细节和复杂统统丢给一个框架,CI 是你最好的朋友。 CI 有很多优点:免费,轻...
CodeIgniter 3中文手册 --CI 3.0 官方简体中文版用户手册.chm 基于最新 2016-09-14. 官方简体中文手册制作, 由于官方没有提供 chm版本的手册, 使用很不方便,所有自己制作了本 CHM ci手册. 免费分享给大家!
Codeigniter Grid 使用方法 (flexigrid) 博客分类: Codeigniter / CakePHP ASPRailsRubyPHPGoogle 来源:http://codeigniter.com/forums/viewthread/75326/P0/ Updated: 6 September 2008 Although I love CI, ...
CodeIgniter-AngularJS-App, 在CodeIgniter和AngularJS上,示例应用程序基于 的CodeIgniter应用程序基于 CodeIgniter 3.x 和 AngularJS 1. x.的样例应用程序这是非常简单的例子- CRUD 。:如何启动?在 appliaction/...
博客 CodeIgniter 网站
代码点火器 我的第一个 CodeIgniter 项目。 使用 codeigniter 创建各种游戏。 内容 “大富乡” 如何开始 待定详情... Git克隆这个docker-compose up -d mysql nginx 移民 使用浏览器访问 。
CodeIgniter 2.2.2 中文文档。 从中文官网抓取。 html压缩包版本。
codeigniter(CI)中文手册下载
// Load library in your controller $this->load->library('infiqr'); // Call Infiqr and pass the content to simply generate image output in your controller $this->infiqr->generate('Hello, testing ...
发布信息此存储库包含将来版本的开发中代码。 要下载最新的稳定版本,请访问页面。变更日志和新功能您可以在 找到每个版本的所有更改的列表。服务器要求建议使用PHP 5.6或更高版本。 它也应该在5.3.7上运行,但是...
CodeIgniter CodeIgniter 的很棒的核心、帮助程序、钩子、语言、库、第三方和其他很酷的资源的列表。 灵感来自 贡献 详情请参阅。 目录 复杂的 - 快速启动您的 CodeIgniter Web 应用程序。 - CodeIgniter 3 的轻量级...
Vign的Codeigniter 4片段 该扩展提供了Codeigniter 4片段 要求 CodeIgniter Version : 4.1.1 安装 启动代码的命令面板 ext install adereksisusanto.codeigniter4-snippets 表中的内容 [新] [新] 路线的替代片段 ...
Codeigniter的面包屑 轻量级Codeigniter库,用于生成HTML面包屑 用法 易于理解和使用的库是编写此库时的主要考虑因素之一。 从这个意义上讲,使用这个简单的库可以轻松应对,从添加和更新配置文件到加载库和用法。 ...
CodeIgniter and Ajax using jQuery.zip (超赞的实例,代码非常简单) ...CodeIgniter资料+ci+jquery范例.zip PHP 敏捷开发框架CodeIgniter.chm Wrox,.Professional.CodeIgniter.(2008).BBL.[0470282452].pdf
点火枪 Codeigniter黑客可快速构建软件应用程序
PHP Codeigniter 3.0 框架的简单库。 CodeIgniter 框架 3.0 的简单库,用于在线定位 ip,无需本地数据库, //www.ipinfodb.com/ API。 ## 指示:1 首先你必须在创建一个免费帐户并获取一个密钥并更改类(应用程序...
CodeIgniter:开源PHP框架(最初来自EllisLab)
Angular-ui-route-use-in-codeigniter 在 Codeigniter 中使用 Angular ui 路由###控制器 <?phpdefined ( 'BASEPATH' ) OR exit ( 'No direct script access allowed' );class Home extends CI_Controller {// ...