gpt4 book ai didi

php - codeigniter 闪存数据未按预期工作

转载 作者:行者123 更新时间:2023-12-02 09:32:02 28 4
gpt4 key购买 nike

我已经在 Controller 中设置了闪存数据

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);
}

在我的 View 文件中(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

我需要在每次收到 $error 时显示 flashdata 消息

最佳答案

千辛万苦终于成功了。您所要做的就是使用 $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');
}

?>

之后在我的 Controller 中 construct 函数

 public function __construct() {
parent::__construct();
.....
$this->session->keep_flashdata('message');
}

它在每个错误中都有效。仍然有一些愚蠢的问题,但到目前为止工作得很好

关于php - codeigniter 闪存数据未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32065825/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com