php CodeIgniter 闪存数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12353370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter Flash Data
提问by Tom
I'm struggling with Flash Data in CodeIgniter.
我正在为 CodeIgniter 中的 Flash 数据苦苦挣扎。
I basically want to:
我基本上想:
add a category to a database redirect user back to a page show a success pop-up message "Your category has been created"
将类别添加到数据库将用户重定向回页面显示成功弹出消息“您的类别已创建”
So far I can add the category successfully to the db and the user input is validated correctly, only thing is I don't know how to create the pop up success message. (I don't want to load a success view), just redirect back to where they came from and show small message in the top corner or something.
到目前为止,我可以将类别成功添加到数据库中,并且用户输入已正确验证,唯一的问题是我不知道如何创建弹出成功消息。(我不想加载成功视图),只需重定向回它们的来源并在顶角或其他地方显示小消息。
Is flash data the right way to go?
闪存数据是正确的方法吗?
回答by Mudshark
In your controller:
在您的控制器中:
//add to db
// load session library if not auto-loaded
$this->session->set_flashdata('msg', 'Category added');
redirect('controller/method');
In the view:
在视图中:
<script>
// assumes you're using jQuery
$(document).ready(function() {
$('.confirm-div').hide();
<?php if($this->session->flashdata('msg')){ ?>
$('.confirm-div').html('<?php echo $this->session->flashdata('msg'); ?>').show();
<?php } ?>
});
</script>
回答by William Kheng
Your can perform different session message depends what you pass to view from your controller. Noted that I am using Bootstrap as my CSS backbone.
您可以执行不同的会话消息取决于您从控制器传递给视图的内容。注意到我使用 Bootstrap 作为我的 CSS 主干。
In view,
在看来,
For success case,
对于成功案例,
<?php if ($this->session->flashdata('category_success')) { ?>
<div class="alert alert-success"> <?= $this->session->flashdata('category_success') ?> </div>
<?php } ?>
For error case,
对于错误情况,
<?php if ($this->session->flashdata('category_error')) { ?>
<div class="alert alert-danger"> <?= $this->session->flashdata('category_error') ?> </div>
<?php } ?>
In controller,
在控制器中,
For success case,
对于成功案例,
$this->session->set_flashdata('category_success', 'Success message.');
redirect("To your view");
For error case,
对于错误情况,
$this->session->set_flashdata('category_error', 'Error message.');
redirect("To your view");
For more reference you can visit: http://www.codeigniter.com/userguide2/libraries/sessions.html
如需更多参考,您可以访问:http: //www.codeigniter.com/userguide2/libraries/sessions.html
回答by Walk
You can try this -
你可以试试这个——
Controller:
控制器:
$this->session->set_flashdata('success', 'Success Message...');
OR
$this->session->set_flashdata('error', 'Error Message...');
OR
$this->session->set_flashdata('warning', 'Warning Message...');
OR
$this->session->set_flashdata('info', 'Info Message...');
View:
看法:
<?php if($this->session->flashdata('success')){ ?>
<div class="alert alert-success">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Success!</strong> <?php echo $this->session->flashdata('success'); ?>
</div>
<?php } else if($this->session->flashdata('error')){ ?>
<div class="alert alert-danger">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Error!</strong> <?php echo $this->session->flashdata('error'); ?>
</div>
<?php } else if($this->session->flashdata('warning')){ ?>
<div class="alert alert-warning">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Warning!</strong> <?php echo $this->session->flashdata('warning'); ?>
</div>
<?php } else if($this->session->flashdata('info')){ ?>
<div class="alert alert-info">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Info!</strong> <?php echo $this->session->flashdata('info'); ?>
</div>
<?php } ?>
回答by saurabh kamble
using ternary operator :
使用三元运算符:
Setting Flash Data:
设置闪存数据:
$this->session->set_flashdata('insertproduct', 'Product added successfully');
$this->session->set_flashdata('deleteproduct','Delete added successfully');
Using the Flash Session Data:
使用 Flash 会话数据:
<?php if($this->session->flashdata('insertproduct')):echo $this->session->flashdata('insert');endif; ?><br/>
<?php if($this->session->flashdata('delete')): echo $this->session->flashdata('delete'); endif;?>
回答by erickie007
//Set Flash messages
$this->session->set_flashdata('post_created', 'Your post has been Posted!');
redirect('Posts/index');
//In Posts View you will have
<?php if($this->session->flashdata('post_created')) : ?>
<?php echo '<p class="alert alert-success"> ' .$this->session->flashdata('post_created'). '</p>'; ?>
<?php endif; ?>
回答by hndr
Yes, just check if the flash data is available, if it is, show the message, if it isn't, then don't show it. as simple as that.
是的,只需检查闪存数据是否可用,如果可用,则显示消息,如果不可用,则不显示。就如此容易。
p.s. you should always do a redirect after a POST request.
ps 你应该总是在 POST 请求后进行重定向。
回答by Phil
CodeIgniter's Flash datautilizes PHPsessionvariables. It places a :oldin the session name so that it only lasts for one db call. It's very function and purpose is to do what you are wanting to do, so, yes, it's a very good way of going about these types of things.
CodeIgniter 的Flash 数据使用PHPsession变量。它将 a:old放在会话名称中,以便它仅持续一次 db 调用。它的功能和目的是做你想做的事,所以,是的,这是处理这些类型事情的好方法。
Remember if you are going to use this you must include
$this->session->library('session')
请记住,如果您要使用它,您必须包括
$this->session->library('session')
If you are not sure how to actually use flash_datathan I would suggest reading the docs I previously linked.
如果您不确定如何实际使用flash_data,我建议您阅读我之前链接的文档。
$this->session->set_flashdata(
'category_success',
'Your category has been created'
);
redirect(); //location
echo $this->session->flashdata('category_success');

