清风徐来
知是行之始,行是知之成

Simple FlashNotice helper for CodeIgniter

PHP CodeIgniter 约 1 分钟

这是一个容易使用的简单的FlashNotice helper。 它会在你进行一些操作之后帮助你显示错误或提示信息。 信息显示一次后就会被自动删除。 1,保持以下代码到“flash_helper.php” application/helpers 目录 2,加载它 $this->load->helper(“flash”);

  • 设置提示信息用 setflash("write notice","notice")
  • 设置错误信息用 setflash("write error information","error");
  • 在你的view文件要显示信息的地方加上 <?php echo flash();?>

你可以在第二个参数上设置其他的通知类型,它会反映在你的提示div块的class名称上。 #flash 样式你可以自己定义。

<?php 
function flash() { 
$flash = $_SESSION["flash"]; 
unset($_SESSION["flash"]);
$str = '';

foreach($flash as $key=>$val) { 
$str .= '<div id="flash" class="'.$key.'">'.$val."</div>"; 
} 

return $str; 

} 
function setflash($error="An error has occurred",$type="error") { 
return $_SESSION["flash"][$type] = $error; 
} 
?>

你可以使用 script.aculo.us , Prototype ,jQquery 等设置过几秒钟自动隐藏提示信息. 例如:

window.onload = setTimeout("new Effect.Fade('flash')",7000); 

jQuery 的写法如下:

<script language="javascript" type="text/javascript">
$(document).ready(function(){ 
	 // 这里是加载完毕要处理的相关代码...
	 setTimeout(function(){$('#flash').hide()},7000);
 });
</script>

2008-10-23 稍稍修正了一下,flash_helper.php 第四行加了$str = ‘’;就是先定义一下str,否则CI会有 Notice 提示