我在控制器中设置了flashdata,如
public function customer() {
$data['title'] = 'Black List Management';
if ($this->input->post('single_black')) {
//echo 'here';return;
$wallet = trim($this->input->post('single_wallet', TRUE));
$reason = trim($this->input->post('reason', TRUE));
$match = preg_match('/^01[15-9]\d{8}$/', $wallet);
//if not valid mobile
if ($match == 0 || !$match) {
$this->session->set_flashdata('message', 'The wallet is not valid Mobile no.');
redirect('blacklist/index');
}
$is_blacklisted = $this->db->where('wallet', $wallet)->where('is_blacklisted', 1)->get('customers')->num_rows();
//if already blacklisted
if ($is_blacklisted > 0) {
$this->session->set_flashdata('message', 'This wallet is already in blacklist');
redirect('blacklist/index');
}
$this->form_validation->set_rules('reason', 'Reason', 'required');
if ($this->form_validation->run() == FALSE) {// if invalid form
$this->nuts_lib->view_loader('user', 'blacklist', $data);
return;
} else {
$user_id = (int) $this->session->user_id;
$query = $this->db->where('wallet', $wallet)->where('is_blacklisted', 0)->get('customers');
$result = $query->result_array();
if (count($result) > 0) {// if exist uppdate
$customer_id = (int) $result[0]['id'];
$blacklist = array(
'is_blacklisted' => 1,
'blacklist_meta' => date('Y-m-d H:i:s') . '|' . $user_id . '|' . $reason
);
$this->db->where('id', $customer_id)->update('customers', $blacklist);
} else {// insert
$new_blacklist = array(
'wallet' => $wallet,
'is_blacklisted' => 1,
'blacklist_meta' => date('Y-m-d H:i:s') . '|' . $user_id . '|' . $reason
);
$this->db->insert('customers', $new_blacklist);
}
$this->session->set_flashdata('message', 'Successfully Blacklisted');
redirect('blacklist');
}
}
}
从customer
方法重定向到错误
public function index() {
$data['title'] = 'Black List Management';
$this->nuts_lib->view_loader('user', 'blacklist', $data);
}
在我的视图文件(user / blacklist.php)
中$message = $this->session->flashdata('message');
if (isset($message)) {
echo '<div class="alert alert-info">' . $message . '</div>';
}
所以,当$error
显示flashdata时,但问题是下次(提交表单后)得到相同的错误,然后flashdata不再显示。
到目前为止,我所尝试的是CodeIgniter flashdata not working after redirect
我需要在获取flashdata
$error
消息时间
答案 0 :(得分:4)
最后,经过长时间的努力,它才有效。您所要做的就是将$this->session->keep_flashdata('message')
与$this->session->unset_userdata('message')
这是我的解决方案(查看文件)
<?php
$message = $this->session->flashdata('message');
if (isset($message)) {
echo '<div class="alert alert-info">' . $message . '</div>';
$this->session->unset_userdata('message');
}
?>
之后在我的控制器construct
功能
public function __construct() {
parent::__construct();
.....
$this->session->keep_flashdata('message');
}
它适用于每个错误。仍然有一些愚蠢的问题,但到目前为止工作得很好
答案 1 :(得分:1)
试试这个..
$message = $this->session->flashdata('message');
if (isset($message)) {
echo '<div class="alert alert-info">' . $message . '</div>';
$this->session->unset_userdata('message');
}
答案 2 :(得分:0)
您面临的问题非常简单。它将仅首次显示,因为如果出现错误,它将不会第二次检查。 它背后的原因是你已经将flash数据放在IF ..ELSE语句中......它只能用于一次。
试试这个:
In [11]: fruit.merge(costs, how="left")
Out[11]:
colour fruit cost
0 red apple 1.7
1 orange orange 1.4
2 green apple 1.7
3 black blueberry 2.1
我认为这对你有用......
答案 3 :(得分:0)
不是在视图中显示Flash消息,而是将其放入控制器并将其传递到视图中
<强>控制器强>
<?php
if($error){
$this->session->set_flashdata('message','number is not valid');
redirect('blacklist/index');
}
function index()
{
$error = $this->session->flashdata('message');// get you flash message
$data = array();// create array
//...
$data['message'] = $error;// pass you message to array
$this->load->view('someview',$data);// pass your message to your view
}
?>
查看强>
// And in your view file
<?php if($message):
echo '<div class="alert alert-info">' . $message . '</div>';
endif; ?>
答案 4 :(得分:0)
我在我在CI中构建的大多数项目中执行此操作。我就是这样做的(HTML标记适用于Foundation);
控制器:
$this->session->set_flashdata('error', 'Invalid registration number');
redirect();
观点:
if ($error = $this->session->flashdata('error')) {
echo '<div data-alert class="alert-box alert">';
if (is_array($error))
{
foreach($error as $e)
{
echo '<p class="error">' . $e . '</p>';
}
}
else
{
echo $error;
}
echo '<a href="#" class="close">×</a>';
echo '</div>';
}
希望这有帮助。
答案 5 :(得分:0)
这是您在CI中设置Flash消息的方式
$this->session->set_flashdata('message','number is not valid');
redirect('blacklist/index'); //Redirect after setting flash message
重定向后在您的视图页面中创建DIV
<div class="confirm-div alert alert-info"></div>
在</body >
<script>
// assumes you're using jQuery
$(document).ready(function() {
$('.confirm-div').hide();
<?php if($this->session->flashdata('message')){ ?>
$('.confirm-div').html('<?php echo $this->session->flashdata('message'); ?>').show();
<?php } ?>
});
</script>
答案 6 :(得分:0)
在您的电子邮件发送控制器中添加此构造函数:
public function __construct() {
parent::__construct();
$this->session->keep_flashdata('message');
}